LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-12-2009, 07:15 AM   #1
raghu_rao
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Rep: Reputation: 0
Forcing static linking of shared libraries


Hi,

I need to statically link a shared library, so that I may distribute the binary without having to bundle in the lib as well.
I am trying to use the "-static" flag but when I do so the library does not get linked at all.
Am using gcc for linking. Running it on linux 2.6.18-92.el5.

Also, How do I force the linker to pick up the .a instead of the .so?
Any help will be much appreciated.
 
Old 01-12-2009, 08:39 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Are you using autotools?
 
Old 01-12-2009, 12:03 PM   #3
raghu_rao
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Original Poster
Rep: Reputation: 0
No, am not.
 
Old 01-12-2009, 12:07 PM   #4
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Could you please post the gcc command you are using?
 
Old 01-13-2009, 01:44 AM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by raghu_rao View Post
Hi,

I need to statically link a shared library, so that I may distribute the binary without having to bundle in the lib as well.
I am trying to use the "-static" flag but when I do so the library does not get linked at all.
Am using gcc for linking. Running it on linux 2.6.18-92.el5.

Also, How do I force the linker to pick up the .a instead of the .so?
Any help will be much appreciated.
You can't statically link shared library (or dynamically link static)

Flag -static will force linker to use static library (.a) instead of shared (.so)
But. Static libraries not always installed by default. So if you need static link you have to install static libraries.

Another possible approach is use statifier (http:/statifier.sf.net) or Ermine (http://magicErmine.com)
Both tools take as input dynamically linked executable and as output create self-contained executable with all shared libraries embedded.

Statifier is free (licensed under GPL), Ermine is commercial, but it behaves better on systems with memory randomization. Here is theirs comparison:
http://www.magicermine.com/products....ure_comparison

Last edited by Valery Reznic; 03-29-2010 at 05:12 PM.
 
Old 01-13-2009, 02:05 AM   #6
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
one thing you can do is, for what ever the libraries you are using, create static libraries while 'make'. install them and use those static libraries.
 
Old 01-14-2009, 06:18 AM   #7
raghu_rao
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Original Poster
Rep: Reputation: 0
@Agrouf:

The command I'm using is something like :
gcc -static -D<symbols> -L<libpath1> -L<libpath2> <list of objs> -o <executable>

@Valery:

The library I am using has both .a and .so versions in the same folder. But -static is not forcing the linker to pick up the .a version.

Will try statifier and see if it solves the issue!

@Lokesh:
the static version of the lib is already present. But the .so is being picked up all the time.

@all
Is there any gcc flag that lets us specify the library with extention?

Thanks for all the help!
 
Old 01-14-2009, 07:32 AM   #8
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by raghu_rao View Post
@Lokesh:
the static version of the lib is already present. But the .so is being picked up all the time.
I don't think so. because as per my knowledge goes, compiler by default picks only the static if it exists. If it not exists then only it looks for shared object.

post the names of the static and shared object libraries
 
Old 01-14-2009, 07:34 AM   #9
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by raghu_rao View Post
@all
Is there any gcc flag that lets us specify the library with extention?
-static-libgcc

look at this option in the man gcc.
 
  


Reply

Tags
library, shared



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
Problem linking static libraries in g++ zbinwu Programming 6 07-26-2007 04:31 PM
linking static libraries??? birjodh Linux - Newbie 3 06-13-2007 02:54 AM
linking static libraries??? birjodh Linux - General 1 06-07-2007 11:02 PM
Linking static and shared libraries in the KDevelop Environment tristanm Programming 15 11-27-2006 09:59 PM
any links on linking, loading shared libraries, shared objects. etc? ananthbv Programming 0 02-03-2005 07:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:02 AM.

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