Luc*_*ncu 76 compiler-construction linker gcc ld object-files
如何将两个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
Run Code Online (Sandbox Code Playgroud)
如果您有权访问源文件,-combineGCC标志将在编译之前合并源文件:
$ gcc -c -combine a.c b.c -o c.o
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于源文件,并且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
Run Code Online (Sandbox Code Playgroud)
Luc*_*ncu 90
传递-relocatable(或-r)ld将创建一个适合作为输入的对象ld.
$ ld -relocatable a.o b.o -o c.o
$ gcc c.o other.o -o executable
$ ./executable
Run Code Online (Sandbox Code Playgroud)
生成的文件与原始.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
Run Code Online (Sandbox Code Playgroud)
小智 8
如果要创建两个或更多.o文件(即静态库)的存档,请使用以下ar命令:
ar rvs mylib.a file1.o file2.o
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48749 次 |
| 最近记录: |