c - 编译C文件时出现Unsupported x86-64 instruction set错误

标签 c gcc assembly

我正在尝试按照 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);
    }
} 

关于如何解决这个问题有什么建议吗?

最佳答案

提供 -m32 而不是 -march=i686

关于c - 编译C文件时出现Unsupported x86-64 instruction set错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679693/

相关文章:

c - 从文件中读取数组大小

c++ - 如何使 g++ 导致异常而不是警告格式错误

c++ - 命令输入文件如何影响 GCC 中的链接和静态初始化?

assembly - 位置参数与关键字参数

C++ 内联汇编 : how to deal with references?

c - 如何修复不需要的诅咒输出缩进

c - 如何将 K&R 函数声明自动转换为 ANSI 函数声明?

assembly - 在汇编中有条件地调用子程序

c - 在字符串数组中搜索字符串

gcc - 如何在代码块上添加编译器标志