Solved

how to link static library to a shared library

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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