Linking a dynamic library to a static library that links to other static libraries

限于喜欢 提交于 2019-12-08 07:41:11

问题


In my C++ application I have a static library (libCOMMON.a) that links to boost libraries: system, filsystem, timer and chrono.

I am using CMake and here is how I create and link libCOMMON.a:

ADD_LIBRARY(COMMON ${COMMON_SRCS})
target_link_libraries(COMMON 
    ${BOOST_LIB_DIR}/libboost_filesystem.a
    ${BOOST_LIB_DIR}/libboost_system.a
    ${BOOST_LIB_DIR}/libboost_timer.a
    ${BOOST_LIB_DIR}/libboost_chrono.a
)

I also have plugins for this application that links to libCOMMON.a. The plugins are built as dynamic libraries. Everything compiles ok (using gcc) but when I start the application, the plugins can't be loaded because some symbols in the dynamic libraries related to boost cannot be resolved.

The solution was to link each of the plugins to boost. Is there a better way ? I thought that if boost libraries are linked statically into libCOMMON.a, it would be enough to link the plugins to libCOMMON.a.

Can someone explain what's happening ?

Thanks


回答1:


I think the problem is that boost libraries are built as dynamic libraries by default. Even if the ".a" suggests that they are built as static libraries, the lib folder of boost contains a ".so" library with each ".a". Which means that libCOMMON.a is linked dynamically to boost libraries. For this reason, the plugins that statically links to libCOMMON.a has also to dynamically link to boost libraries. A better solution would be to build boost libraries as static libraries.



来源:https://stackoverflow.com/questions/10432968/linking-a-dynamic-library-to-a-static-library-that-links-to-other-static-librari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!