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

我正在尝试按照此链接中的教程。

当我开始编写test.c文件的部分时,我尝试运行第一个编译行。

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

这是test.c的内容

 __asm__(".code16\n"); __asm__("jmpl $0x0000, $main\n"); void main() { } 

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

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

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

我正在运行Ubuntu Desktop 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 \ n”);

 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