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

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

问题描述

在gcc (g++) 下,我编译了一个静态.a(称之为some_static_lib.a)库.我想将这个 .a 文件链接(这是正确的短语吗?)我正在构建的另一个动态库(称为 libsomeDyn.so).虽然 .so 可以编译,但我使用 nm 命令在 .so 下看不到 .a 的内容:

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

/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 下没有看到函数.我做错了什么?

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.

在 Linux 上,您可以使用 --whole-archive 链接器选项更改该行为:

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天全站免登陆