Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编译C文件时出现不支持的x86-64指令集错误_C_Gcc_Assembly - Fatal编程技术网

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

编译C文件时出现不支持的x86-64指令集错误,c,gcc,assembly,C,Gcc,Assembly,我正在尝试按照链接中的教程进行操作 当我开始制作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

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

当我开始制作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桌面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

第一个给了我一个错误

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
工作

工作

失败于

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


`在汇编语言指令用于在16位模式下运行之前,.code16'或`.code16gcc'指令。我这样做了,它可以工作,但是教程中还提供了两行编译。第二行“ld-static-Ttest.ld-nostdlib--nmagic-o test.elf test.o”给出了“ld:i386输入文件'test.o'的体系结构与i386:x86-64输出不兼容”的错误。我已经用ld代码对问题进行了编辑,请参见supply
-melf_i386
ld
调用。此答案没有添加任何新内容。这似乎是试图在其他答案中总结所有建议。
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
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