如何將兩個GCC編譯的.o目標文件合併到第三個.o文件中?將兩個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
如果你有機會到源文件的-combine
GCC標誌將編譯前的源文件合併:
$ 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.o
c.o: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, not stripped
$ file a.o
a.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
是否有可能做相反的操作?即從c.o產生a.o和b.o? – 2014-02-01 22:19:06
@BertRegelink否,因爲沒有唯一的逆,在數學術語中,不形成組:P – 2014-02-23 08:03:01
警告:'--relocatable'似乎不太便攜。 Android NDK附帶的ld只識別'-relocatable'。如果你需要可移植性,請堅持'-r'。 – 2017-06-20 15:27:57