Re: [CMake] embedding lib into a shared object

2010-09-29 Thread Marcel Loose
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.


___
Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake


Re: [CMake] embedding lib into a shared object

2010-09-29 Thread Michael Hertling
On 09/29/2010 12:26 PM, 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?

man ld:

[...]
--whole-archive
For each archive mentioned on the command line after the --whole-
archive option, include every object file in the archive in the
link, rather than searching the archive for the required object
files. This is normally used to turn an archive file into a
shared library, forcing every object to be included in the
resulting shared library. This option may be used more than once.

Two notes when using this option from gcc: First, gcc doesn’t know
about this option, so you have to use -Wl,-whole-archive. Second,
don’t forget to use -Wl,-no-whole-archive after your list of
archives, because gcc will add its own list of archives to your
link and you may not want this flag to affect those as well.
[...]

E.g., you can use the LINK_FLAGS target property to enable this option,
and don't forget -fPIC and 'extern "C"' for functions.cpp's dlopening.

Besides, the ADD_DEPENDENCIES() is not necessary because of the
TARGET_LINK_LIBRARIES(), and wouldn't it be worth considering to
compile the dl-suitable module from the static library's sources?

Regards,

Michael
___
Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake