[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to create statically linked SO in Linux?

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

649 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