LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   LINUX - linking archive (static library) with shared (dynamic) library (https://www.linuxquestions.org/questions/programming-9/linux-linking-archive-static-library-with-shared-dynamic-library-467565/)

gurkama 07-25-2006 03:27 PM

LINUX - linking archive (static library) with shared (dynamic) library
 
Hi all,

I want to create an archive from a bunch of object files that should be linked with an existing shared library. Meaning that I would like to link the archive with a shared library.

Can it be done? If so - how can I do that?

I'm working with GNU Linux 2.6.

Thanks

Matir 07-25-2006 03:30 PM

The object files themselves would need to be linked against the shared library, AFAIK.

gurkama 07-25-2006 03:45 PM

Thanks Matir for your quick response.

However, I still don't get what you meant:
When creating the objects there's no linkage - I only compile the c files into object files using implicit rule. How can I link the objects with a shared library as you kindly suggested?

Thanks

Matir 07-25-2006 05:00 PM

I believe you'd need to use:
Code:

gcc -shared -Wl,-soname,LIBNAME -o LIBNAME OBJFILES -lOTHERLIBRARY

twist 09-01-2006 02:48 PM

Quote:

Originally Posted by Matir
I believe you'd need to use:
Code:

gcc -shared -Wl,-soname,LIBNAME -o LIBNAME OBJFILES -lOTHERLIBRARY

I'm trying to build a .so from a .a like so:

Code:

ar r foo.a foo.o
gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 foo.a

A .so is produced but linking against it results in undefined references to globals in foo.o. However just using the .o ...

Code:

gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 foo.o
works fine and all global symbols in foo.o are found.

What do I need to do to get the linux linker to see globals in foo.a? I tried a version script but either I got it wrong or I'm missing something.

Ultimately I have two goals:

1) produce a .so from a .a
2) control which symbols are visible (i.e. global) in the .so

FWIW, I'm coming from an AIX background where .a's work in this context and symbol visibilty can be controlled via .exp files.

Any help would greatly be appreciated.

Thanks.

negiliblek 03-04-2007 11:11 PM

script to change static .a to a shared .so
 
can't say I know exactly what you're talking about but here is a site where the script below came from homes.esat.kuleuven.be/~gcc/shared_libraries.html

(Note this site also discusses stripping unwanted symbols out)

# Converting a static to a shared library:

#! /usr/bin/ksh -p
#
# Makes a shared library from a static one
#
static_library=$1; shared_library=$2
ld -shared -s -h /freeware/gcc/linux-i386/lib/{shared_library} -o ${shared_library:-${static_library%%a}so} -whole-archive ${static_library}


I'm testing this script right now on an archive libmpeg3.a

Quote:

Originally Posted by twist
I'm trying to build a .so from a .a like so:

Code:

ar r foo.a foo.o
gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 foo.a

A .so is produced but linking against it results in undefined references to globals in foo.o. However just using the .o ...

Code:

gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 foo.o
works fine and all global symbols in foo.o are found.

What do I need to do to get the linux linker to see globals in foo.a? I tried a version script but either I got it wrong or I'm missing something.

Ultimately I have two goals:

1) produce a .so from a .a
2) control which symbols are visible (i.e. global) in the .so

FWIW, I'm coming from an AIX background where .a's work in this context and symbol visibilty can be controlled via .exp files.

Any help would greatly be appreciated.

Thanks.



All times are GMT -5. The time now is 09:32 PM.