【问题标题】:combine two GCC compiled .o object files into a third .o file将两个 GCC 编译的 .o 目标文件合并到第三个 .o 文件中
【发布时间】:2011-02-28 03:15:28
【问题描述】:

如何将两个 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

【问题讨论】:

  • gcc 目前没有-combine 选项。它存在于 gcc 4.1.2 中并且不存在于 gcc 6.3.0 中(其他人可以弄清楚它何时被删除)。

标签: gcc compiler-construction linker ld object-files


【解决方案1】:

-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

【讨论】:

  • 是否可以进行逆运算?即从 c.o 产生 a.o 和 b.o?
  • @BertRegelink 不,因为没有唯一的逆,在数学方面,不形成组:P
  • 警告:--relocatable 似乎不太便携。 Android NDK 自带的 ld 只识别-relocatable。如果您需要便携性,请坚持-r
  • @matthijs 这个词是一样的;差是一减二。
  • 啊,没看到。所以,Android NDK 只能识别-relocatable -r,但不能识别--relocatable。感谢您的澄清!
【解决方案2】:

如果您想创建包含两个或多个 .o 文件的存档(即静态库),请使用 ar 命令:

ar rvs mylib.a file1.o file2.o

【讨论】:

  • @Lucian 但是你为什么要这样做呢?静态库比 .o 文件更方便链接。
  • 我需要在生成的文件上运行objcopy 并在文件中创建一些本地符号,以便它们在外部不可见。在a.ob.o 文件之间引用了一些需要本地化的符号。我无法本地化单个文件——因为在链接器时找不到符号——我也无法本地化静态存档中的符号。
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多