?
Solved

How to create statically linked SO in Linux?

Posted on 2012-04-13
5
Medium Priority
?
491 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
4 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 1600 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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