ld - 输出文件为空

ld - output file is empty

我正在尝试用汇编和 C++ 编写内核(我现在还在汇编),我正在用 GRUB 加载它。到目前为止,一切正常,除了链接。我正在使用 ld,即使输入文件不为空,它也会生成一个空二进制文件。

这是我的构建脚本:

@echo off
nasm -f elf64 multiboot_header.asm
nasm -f elf64 boot.asm
ld --nmagic --output=kernel.bin --script=linker.ld multiboot_header.o boot.o
move /Y kernel.bin isofiles/boot/kernel.bin
wsl grub-mkrescue -o os.iso isofiles
rem del *.o

和我的 linker.ld 脚本:

ENTRY(start)

SECTIONS {
    . = 1M;

    .boot :
    {
        /* ensure that the multiboot header is at the beginning */
        *(.multiboot_header)
    }

    .text :
    {
        *(.text)
    }
}

编辑: 我不小心使用了 MinGW 的 ld,而不是 Linux gcc。

正如您所说:不小心使用 MinGW 中的 ld.exe 可能会导致这种情况发生(与 Linux gcc 中的 ld.exe 相反)