Solved

how to link static library to a shared library

Posted on 2001-07-12
4
2,037 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 100 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now