Linking a dynamic library to a static library that links to other static libraries
Asked Answered
C

1

2

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

Cartan answered 3/5, 2012 at 13:59 Comment(4)
Can you show the command that was used to produce libCOMMON.a? There's a few ways you can solve this one.Edgerton
I am using CMake to do that: ADD_LIBRARY(COMMON ${COMMON_SRCS}). I have edited the question and added more details.Cartan
Static libraries cannot be "linked into" other static libraries. A static library is just a collection of object files. There's no mechanism in place to reference other libraries. You could create a big library out of several smaller libraries, but that would be a pointless exercise in duplication of information.Centenarian
Well my plugins doesn't need to know about boost. They use some functionalities of boost and/or other libraries assembled in some interface objects (see Facade design pattern) available in libCOMMON.Cartan
C
2

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.

Cartan answered 8/5, 2012 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.