nbilal
nbilal

Reputation: 1099

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

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

Upvotes: 2

Views: 2173

Answers (1)

nbilal
nbilal

Reputation: 1099

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.

Upvotes: 2

Related Questions