如何将静态库链接到gcc的动态库中 [英] how to link static library into dynamic library in gcc

查看:139
本文介绍了如何将静态库链接到gcc的动态库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gcc(g ++)下,我编译了一个静态的 .a (称之为 some_static_lib.a )库。我想将这个 .a 文件链接到另一个动态库(称为 libsomeDyn.so .so ,但在 .so下没有看到 .a 的内容使用 nm 命令:


/ usr / bin / g ++ -fPIC -g -O2 -Wall -Werror -pipe -march = pentium3
-mtune = prescott -MD -D_FILE_OFFSET_BITS = 64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE -I ../../../。 ./../../../../ -I ../../../../../../../..// libraries -Wl,-rpath,/ usr / lib -o libsomeDyn.so some.o another.o some_static_lib.a -shared -Wl -x
-Wl,-soname,libsomeDyn.so


libsomeDyn.so 下的 some_static_lib.a 下没有看到函数。我在做什么错了?

解决方案

静态库在链接时有特殊的规则。如果对象提供了未解析的符号,则静态库中的对象只会添加到二进制文件中。



在Linux上,您可以使用 - whole-archive 链接器选项:

  g ++ -Wl, - 整个归档some_static_lib.a -Wl, -  no-whole-archive 


Under gcc (g++), I have compiled a static .a (call it some_static_lib.a) library. I want to link (is that the right phrase?) this .a file into another dynamic library (call it libsomeDyn.so) that I'm building. Though the .so compiles, I don't see content of .a under .so using nm command:

/usr/bin/g++ -fPIC -g -O2 -Wall -Werror -pipe -march=pentium3 -mtune=prescott -MD -D_FILE_OFFSET_BITS=64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE -I../../../../../../../../ -I../../../../../../../..//libraries -Wl,-rpath,/usr/lib -o libsomeDyn.so some.o another.o some_static_lib.a -shared -Wl -x -Wl,-soname,libsomeDyn.so

I do not see functions under some_static_lib.a under libsomeDyn.so. What am I doing wrong?

解决方案

Static libraries have special rules when it comes to linking. An object from the static library will only be added to the binary if the object provides an unresolved symbol.

On Linux, you can change that behavior with the --whole-archive linker option:

g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive

这篇关于如何将静态库链接到gcc的动态库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆