【问题标题】:elf.h: No such file or directory when using the x86_64-w64-mingw32-gcc cross-compilerelf.h:使用 x86_64-w64-mingw32-gcc 交叉编译器时没有这样的文件或目录
【发布时间】:2026-02-14 05:20:07
【问题描述】:

许多开发人员告诉我使用交叉编译器来编译我的操作系统。 问题是当我从使用gcc 切换到使用x86_64-w64-mingw32-gcc 时,它给了我这篇文章标题中的错误。

我在 Windows 上使用 WSL,我检查了 /usr/include 以查看 elf.h 是否存在,并且确实存在。所以我尝试在使用-I/usr/include 编译时包含该目录,但它仍然不起作用。

PS:我试图在 kernel.cpp 中调用我的 __main 函数,并且我需要 elf.h 来获取标题条目(除非有一种方法可以在没有 elf.h 的情况下做到这一点,我不知道大约)。

完整的引导加载程序生成文件:

all:
    x86_64-w64-mingw32-gcc -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -c -o build/main.o c/main.c
    x86_64-w64-mingw32-gcc -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -c -o build/data.o gnu-efi/lib/data.c
    x86_64-w64-mingw32-gcc -nostdlib -Wl,-dll -shared -Wl,--subsystem,10 -e efi_main -o essentials/main.efi build/main.o build/data.o

我正在使用 UEFI Bare Bones(仅限 GNU-EFI 标头)。

有没有办法阻止错误,或者有没有使用elf.h的替代方法?

感谢您的帮助!

【问题讨论】:

    标签: operating-system cross-compiling mingw32


    【解决方案1】:

    我通过手动将结构添加到我的main.c 文件来解决此问题:

    typedef uint64_t Elf64_Addr;
    
    #define ELFMAG      "\177ELF"
    #define SELFMAG     4
    #define EI_CLASS    4       /* File class byte index */
    #define ELFCLASS64  2       /* 64-bit objects */
    #define EI_DATA     5       /* Data encoding byte index */
    #define ELFDATA2LSB 1       /* 2's complement, little endian */
    #define ET_EXEC     2       /* Executable file */
    #define PT_LOAD     1       /* Loadable program segment */
    #ifdef __x86_64__
    #define EM_MACH     62      /* AMD x86-64 architecture */
    #endif
    #ifdef __aarch64__
    #define EM_MACH     183     /* ARM aarch64 architecture */
    #endif
    
    
    typedef struct
    {
        uint8_t  e_ident[16];   /* Magic number and other info */
        uint16_t e_type;        /* Object file type */
        uint16_t e_machine;     /* Architecture */
        uint32_t e_version;     /* Object file version */
        uint64_t e_entry;       /* Entry point virtual address */
        uint64_t e_phoff;       /* Program header table file offset */
        uint64_t e_shoff;       /* Section header table file offset */
        uint32_t e_flags;       /* Processor-specific flags */
        uint16_t e_ehsize;      /* ELF header size in bytes */
        uint16_t e_phentsize;   /* Program header table entry size */
        uint16_t e_phnum;       /* Program header table entry count */
        uint16_t e_shentsize;   /* Section header table entry size */
        uint16_t e_shnum;       /* Section header table entry count */
        uint16_t e_shstrndx;    /* Section header string table index */
    } Elf64_Ehdr;
    
    typedef struct
    {
        uint32_t p_type;        /* Segment type */
        uint32_t p_flags;       /* Segment flags */
        uint64_t p_offset;      /* Segment file offset */
        uint64_t p_vaddr;       /* Segment virtual address */
        uint64_t p_paddr;       /* Segment physical address */
        uint64_t p_filesz;      /* Segment size in file */
        uint64_t p_memsz;       /* Segment size in memory */
        uint64_t p_align;       /* Segment alignment */
    } Elf64_Phdr;
    

    我在哪里找到这些结构: https://gitlab.com/bztsrc/posix-uefi/-/blob/master/examples/0F_exit_bs/exit_bs.c

    如果您的/usr/include 目录中有文件elf.h,您也可以从该文件中获取这些结构。

    您可以将文件复制到您的首选文件夹,如下所示:

    cd some_dir/preferred_dir
    cp /usr/include/elf.h .
    

    然后复制其中的结构并将它们粘贴到您的项目中。

    如果您使用的是 Windows,则可以使用 WSL 来执行此操作。

    这是我学到的。

    【讨论】:

      最近更新 更多