[CMake] embedding lib into a shared object

Marcel Loose loose at astron.nl
Wed Sep 29 07:11:58 EDT 2010


On Wed, 2010-09-29 at 12:26 +0200, Ovanes Markarian wrote:
> Hello *,
> 
> 
> I have some library available as a Linux lib file. Now I need to
> create a shared object (actually a MODULE in terms of CMake) out of
> this lib. The module is later on loaded using the dlopen-API function.
> 
> 
> I created a sample project with
> 
> 
> /
> +-- testlib
> +-- so
> 
> 
> 
> 
> testlib - consists of a single cpp file haveing a global variable and
> a function returning it.
> 
> 
> CMake script:
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> 
> 
> PROJECT(testlib CXX)
> 
> 
> add_library(testlib STATIC functions.cpp)
> 
> 
> 
> 
> so - consists of a cpp file and links against testlib.
> 
> 
> CMake script:
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> 
> 
> PROJECT(so CXX)
> 
> 
> add_library(so MODULE main.cpp)
> target_link_libraries(so testlib)
> set_target_properties (so PROPERTIES VERSION 1.0 LINKER_LANGUAGE CXX)
> 
> 
> and the Root CMakeLists.txt which should build both:
> 
> 
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> 
> 
> PROJECT(dll_test CXX)
> 
> 
> add_subdirectory(lib)
> add_subdirectory(so)
> add_dependencies(so lib)
> 
> 
> 
> 
> Now my problem is that after the build I can find the symbols from
> functions.cpp in the lib file using the nm tool. But these symbols are
> not part of the so. How can I force them to be part of the so-Module?
> 
> 
> 
> 
> Thanks in advance,
> Ovanes
> 
Hi Ovanes,

Short answer: you can't.
This has been discussed several times on the mailing list. 

Longer answer: you can do it, but not in a portable way.
Check the wiki: http://www.itk.org/Wiki/CMake_FAQ#Library_questions.
There, you can also read that it is possible to do what you want if you
want to use GNU GCC/LD-specific features like the link option
--whole-archive. Needless to say that that's not portable. Besides, for
this to work, you must have compiled your sources with -fPIC -DPIC which
is not the default when building static libraries.

HTH,
Marcel Loose.




More information about the CMake mailing list