编译 C 文件时不支持的 x86-64 指令集错误



我正在尝试按照此链接中的教程进行操作。

当我开始制作test.c文件时,我尝试运行第一行编译。

gcc -c -g -Os -march=i686 -ffreestanding -Wall -Werror test.c -o test.o

以下是test.c的内容

__asm__(".code16n");
__asm__("jmpl $0x0000, $mainn");
void main() {
}

当我调用第一个编译行时,它向我显示此错误。

test.c:1:0: error: CPU you selected does not support x86-64 instruction set
 __asm__(".code16n");
 ^

谁能告诉我为什么会这样?如果可能的话,如何解决它?

我正在运行 Ubuntu 桌面 x64,提前感谢您的帮助。

编辑:

我已将第一行编译更改为:

gcc -c -g -Os -m32 -ffreestanding -Wall -Werror test.c -o test.o

而且它似乎工作正常。但是,还有两条线给我带来了麻烦。

ld -static -Ttest.ld -nostdlib --nmagic -o test.elf test.o

objcopy -O binary test.elf test.bin

第一个抛给我的错误。

ld: i386 architecture of input file `test.o' is incompatible with i386:x86-64 output

正因为如此,我没有尝试最后一行编译。

下面是 test.ld 文件的代码。

ENTRY(main);
SECTIONS
{
    . = 0x7C00;
    .text : AT(0x7C00)
    {
        *(.text);
    }
    .sig : AT(0x7DFE)
    {
        SHORT(0xaa55);
    }
} 

关于如何解决此问题的任何建议?

供应-m32而不是-march=i686

实际上添加-m32你可以保持 -march=i686 ...

gcc -c -g -Os -march=i686 -m32 -ffreestanding -Wall -Werror test.c -o test.o

工程

gcc -c -g -Os -march=i686 -m16 -ffreestanding -Wall -Werror test.c -o test.o

工程

gcc -c -g -Os -march=i686 -m64 -ffreestanding -Wall -Werror test.c -o test.o

失败,并显示 ;

test.c:1:0:错误:您选择的 CPU 不支持 x86-64 指令集 asm(".code16");

gcc -std=c99 -c -g -Os -march=i686 -m32 -ffreestanding -Wall -Werror test.c -o test.o
ld -static -T test.ld -m elf_i386 -nostdlib --nmagic -o test.elf test.o

最新更新