如何将两个由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
-combine
选项。它存在于gcc 4.1.2中,但不存在于gcc 6.3.0中(其他人可以弄清楚它是何时被删除的)。 - Keith Thompson