[英]Building shared library from static libraries
I've written some code that works as a wrapper around boost::asio
and I am looking for a way to pack it as a shared object (.so), to be used by some applications I'm working on, however I would like to remove all dependencies from the boost libraries (mainly because they were custom compiled, and may cause conflict with default pre-built boost libraries). 我已经写了一些代码来作为
boost::asio
的包装,我正在寻找一种将其打包为共享对象(.so)的方法,供正在使用的某些应用程序使用,但是我会希望从boost库中删除所有依赖项(主要是因为它们是自定义编译的,并且可能与默认的预构建boost库发生冲突)。 I'm linking my code to the static versions of these special boost libraries, however, the linker provides the following error: 我将代码链接到这些特殊boost库的静态版本,但是,链接器提供以下错误:
g++ -D_GLIBCXX_USE_CXX11_ABI=0 -I/path_to_boost_headers -Wall -fPIC -o build/obj.o -c include/source.cpp
g++ -D_GLIBCXX_USE_CXX11_ABI=0 -shared -Wl,-soname,libobj.so.1 -o lib/libobj.so.1 build/obj.o -lc /project_path/lib/libboost_serialization.a /project_path/lib/libboost_wserialization.a /project_path/lib/libboost_system.a
/usr/bin/ld: /home/joao/Work/ASBG/code/cpp/extra/socket/lib/libboost_system.a(error_code.o): relocation R_X86_64_PC32 against symbol `_ZN5boost6system16generic_categoryEv' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
If I'm not mistaken, boost libraries are compiled with -fPIC
by default. 如果我没记错的话,默认情况下,boost库是使用
-fPIC
编译的。 Even so, I added the flag when compiling boostand replaced the libraries: same output. 即便如此,我在编译boost时添加了标志并替换了库:相同的输出。
Try to put -Wl,--whole-archive
and -Wl,--no-whole-archive
around the static libraries. 尝试在静态库周围放置
-Wl,--whole-archive
和-Wl,--no-whole-archive
。
Untested, but something like this could work: 未经测试,但是类似这样的方法可能有效:
g++ -D_GLIBCXX_USE_CXX11_ABI=0 -shared -Wl,-soname,libobj.so.1 \
-o lib/libobj.so.1 build/obj.o -lc \
-Wl,--whole-archive \
/project_path/lib/libboost_serialization.a \
/project_path/lib/libboost_wserialization.a \
/project_path/lib/libboost_system.a \
-Wl,--no-whole-archive
Notes: 笔记:
--whole-archive
option forces the linker to look at all symbols and not just at unresolved ones. --whole-archive
选项强制链接器查看所有符号,而不仅仅是未解析的符号。 On Linux, there is the concept of weak and strong symbols. (Also be aware that mixing dynamically and shared libraries is probably not a good idea. You may end up with the combined disadvantages, which goes against your idea of simplification. However, I do not know the whole picture. Just an opinion.) (还请注意,动态混合和共享库可能不是一个好主意。您可能最终会面临综合弊端,这与您的简化想法背道而驰。但是,我不了解整个情况。只是一种意见。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.