[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how to link static library to a shared library

Posted on 2001-07-12
4
Medium Priority
?
2,066 Views
Last Modified: 2009-12-16
Hello,
        I am trying to build a shared library. This library in turn makes a call to static library provided by the Operating system. Please let me know how do i link this static library to the shared library.
Finally this shared library has to be linked to an executable.

Best regards
Narayan
0
Comment
Question by:s_narayan
[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
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
kejin earned 400 total points
ID: 6279933
On linux, the socalled static libraries are actually archive files (.a files). Each archive file contains a group of relocatable object files (.o files). If all these .o files are compiled with position independent code option (with -fpic or -fPIC gcc option), then, you can simply extract all of them from the .a (using `ar -x ...`  i believe) and link them into a .so shared library. On linux, the linking option should be -shared.  The brief steps are:

1. extract .o files from the static library:

     % ar -x mystaticlib.a

2. link all .o files into a shared library:

   % ld -shared *.o -o mydynlib.so -l....

Hope this helps
kejin
0
 
LVL 2

Expert Comment

by:kejin
ID: 6279937
Also, if you only need to link static library into the shared library, you don't need to extract all
of .o from it, you can simply use

   % ld -shared -o mydynlib.so -Lmystaticlibname

Assume your static library name is libmystaticlibname.a

kejin
0
 
LVL 5

Expert Comment

by:bryanh
ID: 6282501
That -L should be -l, and you also need to include the main object files in the link!  E.g.

ld -shared -o mydynlib.so -lmystaticlibname dynlib1.o dynlib2.o ...

And I would use gcc instead of ld as the linker.  Gcc uses ld for its linking, but is usually configured to add necessary search paths and other options to the ld invocation.

And the code in the static library doesn't have to be position independent (i.e. compiled with gcc -fpic).  If it isn't, the shared library won't be as shared as you might like, though.  It will contain more copy-on-write areas.
0
 

Expert Comment

by:SpideyMod
ID: 8275946
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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