LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2012, 03:44 PM   #1
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Rep: Reputation: Disabled
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
 
Old 07-12-2012, 02:50 AM   #2
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
Any ideas - suggestions - anybody?
 
Old 07-12-2012, 03:02 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That's what libtool is good for.
http://www.gnu.org/software/libtool/manual/libtool.html
 
1 members found this post helpful.
Old 07-12-2012, 03:32 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
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

.

Last edited by knudfl; 07-12-2012 at 03:33 AM.
 
Old 07-12-2012, 04:22 AM   #5
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-12-2012, 04:52 AM   #6
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
【how to link to static libs when compiling with cmake or autoconf】 zcnnbb Linux - General 1 05-03-2012 02:04 AM
[SOLVED] 【how to link to static libs when calling cmake or autoconf】 zcnnbb Linux - Newbie 1 03-08-2012 12:17 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
Qt Libs Static or Shared magicm Slackware 2 10-15-2003 05:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration