LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CMake: link multiple archives (static libs) into shared library (https://www.linuxquestions.org/questions/programming-9/cmake-link-multiple-archives-static-libs-into-shared-library-4175415116/)

taylor_ma 07-05-2012 03:44 PM

CMake: link multiple archives (static libs) into shared library
 
Hi,

I have the following structure in my source code:

Code:

MyProject
|
+---mylib
|  |
|  +---module1
|  |
|  +---module2
|  |
|  +---module...
|
+---myExe1
|
+---myExe2

The code in each module builds cleanly for itself. I'd like to build each module into its own libmodulen.a, which is no big deal with cmake - I just create a STATIC library.

On "mylib"-level I have just ADD_SUBDIRECTORY commands, so that cmake descends into each module. Now I'd like to bundle all modules' .a files into one single shared library libmylib.so.

How can I do that using cmake?

The shared library shall then be used to link all executables against.

Cheers
Matt

taylor_ma 07-12-2012 02:50 AM

Any ideas - suggestions - anybody? :cry:

NevemTeve 07-12-2012 03:02 AM

That's what libtool is good for.
http://www.gnu.org/software/libtool/manual/libtool.html

knudfl 07-12-2012 03:32 AM

No solution. Some hints.
First : Static and shared libraries are built in two different ways.
An object.o static cannot be used for a shared library.
... And vice verse : A shared library lib.so renamed to lib.a
will not be recognized as a static library.
http://tldp.org/HOWTO/Program-Librar...libraries.html
http://tldp.org/HOWTO/Program-Librar...libraries.html

The principle of making "shared" with gcc :
gcc -shared -Wl,-soname,your_soname -o library_name file_list library_list
... where file_list can use files.o from multiple named directories.
( Libtool may be the preferred method, see post #2.)

CMAKE links, post #2 here ..
http://www.linuxquestions.org/questi...bdir-942797/#2

.

taylor_ma 07-12-2012 04:22 AM

Yeah, I added something like this but I think it's kinda ugly, i.e. when using a tool like cmake:

Code:

ADD_CUSTOM_TARGET(lib1 ALL gcc -shared -o libtest.so -Wl,--whole-archive module1/libmodule1.a module2/libmodule2.a -Wl,--no-whole-archive)

LINK_DIRECTORIES(.)

ADD_EXECUTABLE(myexe test)

I see, cmake 2.8.8 has a new option OBJECT for ADD_LIBRARY, which maybe something I need.

Unfortunately, I can't try it, as I am fixed to using version 2.8.0 at the moment.

Anyone having experience with that specific option?

taylor_ma 07-12-2012 04:52 AM

Another, cleaner option is

Code:

FILE(GLOB MODULE1_SOURCES module1/*.c)
FILE(GLOB MODULE2_SOURCES module2/*.c)

ADD_LIBRARY(test SHARED ${MODULE1_SOURCES} ${MODULE2_SOURCES})

I think I go with this one, instead of building the modules separtely... :scratch:


All times are GMT -5. The time now is 12:36 PM.