2014-12-28 55 views
4

我正在嘗試關注this鏈接中的教程。編譯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); 
    } 
} 

有關如何解決此問題的任何建議?

+0

'.code16「或'.code16gcc」彙編語言指令之前指令用於在16位模式下運行。 –

回答

9

供應-m32而不是-march=i686

+0

我這樣做,它的工作原理,但是在本教程中還有兩行編譯。第二行'ld -static -Ttest.ld -nostdlib -nmagic -o test.elf test.o'給了我'輸入文件'測試的'ld:i386體系結構'的錯誤。o'與i386:x86-64輸出不兼容'。我使用ld代碼對編輯進行了編輯,以供您查看 –

+2

將'-melf_i386'提供給'ld'調用。 – fuz

4

其實添加-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: error: CPU you selected does not support x86-64 instruction set asm(".code16\n");

-2
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 
+1

此答案不會添加任何新內容。這似乎試圖總結其他答案中的所有建議。 –