一個人如何將兩個由GCC編譯的.o目標文件合并到第三個.o文件中?$ gcc -c a.c -o a.o$ gcc -c b.c -o b.o$ ??? a.o b.o -o c.o$ gcc c.o other.o -o executable如果您有權訪問源文件,則-combineGCC標志將在編譯之前合并源文件:$ gcc -c -combine a.c b.c -o c.o但是,這僅適用于源文件,GCC不接受.o文件作為此命令的輸入。通常,鏈接.o文件無法正常工作,因為您無法使用鏈接器的輸出作為輸入。結果是一個共享庫,并且沒有靜態鏈接到生成的可執行文件中。$ gcc -shared a.o b.o -o c.o$ gcc c.o other.o -o executable$ ./executable./executable: error while loading shared libraries: c.o: cannot open shared object file: No such file or directory$ file c.oc.o: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, not stripped$ file a.oa.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
3 回答

翻閱古今
TA貢獻1780條經驗 獲得超5個贊
傳遞-relocatable或-r以ld將創建一個對象,它是適合作為輸入ld。
$ ld -relocatable a.o b.o -o c.o
$ gcc c.o other.o -o executable
$ ./executable
生成的文件與原始.o文件的類型相同。
$ file a.o
a.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
$ file c.o
c.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
- 3 回答
- 0 關注
- 1536 瀏覽
添加回答
舉報
0/150
提交
取消