Solved

How to create statically linked SO in Linux?

Posted on 2012-04-13
5
453 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
ID: 37841948
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
ID: 37842030
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
ID: 37842052
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
ID: 37842067
>> 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWS CLI - Issue with name display 2 51
Remove a folder in Linux 9 91
Adding more CPU cores to a Linux VM 5 66
Choosing CentOS 16 59
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

861 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

29 Experts available now in Live!

Get 1:1 Help Now