Solved

how to link static library to a shared library

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

719 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