【问题标题】:Unsupported x86-64 instruction set error when compiling C file编译 C 文件时出现不支持的 x86-64 指令集错误
【发布时间】:2014-12-28 19:09:02
【问题描述】:

我正在尝试遵循 this 链接中的教程。

当我开始制作 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);
    }
} 

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

【问题讨论】:

  • ` .code16 ' 或 ` .code16gcc ' 指令之前的汇编语言指令用于在 16 位模式下运行。

标签: c gcc assembly


【解决方案1】:

提供-m32 而不是-march=i686

【讨论】:

  • 我做了这个并且它有效,但是教程中给出了另外两行编译。第二行 '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 输出”。我已经用 ld 代码对问题进行了编辑,供您查看
  • -melf_i386 提供给ld 调用。
【解决方案2】:

其实添加-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");

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

    • 这个答案没有添加任何新内容。这似乎是试图总结其他答案中的所有建议。
    猜你喜欢
    • 2012-02-26
    • 2020-09-12
    • 2012-10-08
    • 1970-01-01
    • 2012-05-02
    • 2020-06-30
    • 2012-03-06
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多