在[一個操作系統的實現]書中,第五章,開始編譯elf文件格式的操作系統內核。
將hello.asm編譯為hello.o,再編譯為可執行文件
編譯hello.o時,nasm報錯is incompatible with i386:x86-64 output。
原因是原實驗是在32位操作系統下做的,使用的庫也是32位的,而現在使用的64位操作系統。
解決方法:
修改上述兩步編譯的寫法:
nasm -f elf64 -g -F stabs sandbox.asm -o sandbox.o
ld -o sandbox sandbox.o
也可以生成兼容32位的應用程序,再編譯為可執行文件:
nasm -f elf -g -F stabs sandbox.asm -o sandbox.o
ld -m elf_i386 -o sandbox sandbox.o
順便,gcc編譯也會出現類似問題
/usr/bin/ld: warning: i386 architecture of input file `./src/main.o' is incompatible with i386:x86-64 output
出現這種警告的時候的時候,運行程序老是段錯誤,要加-ms32,就好了。
gcc -m32 -o usehello_static usehello.c libhello.a