?
Solved

how to link static library to a shared library

Posted on 2001-07-12
4
Medium Priority
?
2,079 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

807 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