Solved

How to create statically linked SO in Linux?

Posted on 2012-04-13
5
448 Views
Last Modified: 2012-04-13
Dear Experts,

I would like to create a so (shared object file) using C++ in Linux such that all the dependency libraries are already included in it. (will be compiled by gcc) What I want to achieve is even the dependency libraries lacking in the target machine, the so will be callable.

BR
0
Comment
Question by:GurcanK
  • 3
5 Comments
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Then, when you build your SO just statically link in all the static libs you need. You'll need to specify the linker option "--whole-archive" otherwise the linker will only pull in symbols your SO actually uses.

You can do that, thus:  g++ <stuff> -Wl,--whole-archive

http://linux.die.net/man/1/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.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
I'm curious, why the B grade? You didn't ask for any more help and it would seem to me my answer is pretty complete.
0
 

Author Comment

by:GurcanK
Comment Utility
OK, if there is a way to change I will change to "Excellent" if you could supply me a real life example of g++ with static linking.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
Comment Utility
>> if you could supply me a real life example of g++ with static linking.

Not really quite sure what you mean by a "real life example". I've explained the syntax and given you a link to the documentation. I can give you an example of using using it if that is what you mean (although it won't be real life as I don't have a real life project to hand with which to do this). Here goes....

g++ mydso.cpp -Wl,-whole-archive -lstaticlib1 -lstaticlib2 -Wl,-no-whole-archive -lotherlib1 -lotherlib2

In this example, staticlib1 and staticlib2 will be wholly included into your DSO (Dynamic Shared Object) but otherlib1 and otherlib2 will not (only the symbols mydso needs will be pulled from them).

Does that help?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now