Solved

How to create statically linked SO in Linux?

Posted on 2012-04-13
5
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Indy 10 not Receiving UDP broadcast 3 43
Post Clonezilla image restore issue 6 65
Disabling security updates Ubuntu 3 45
Linux 3 35
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

749 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