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

nbilal
  • 1,069
  • 2
  • 10
  • 21
  • Can you show the command that was used to produce libCOMMON.a? There's a few ways you can solve this one. – Flexo May 03 '12 at 14:02
  • I am using CMake to do that: ADD_LIBRARY(COMMON ${COMMON_SRCS}). I have edited the question and added more details. – nbilal May 03 '12 at 14:12
  • 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. – n. m. could be an AI May 03 '12 at 16:18
  • 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. – nbilal May 03 '12 at 17:46

1 Answers1

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.

nbilal
  • 1,069
  • 2
  • 10
  • 21