Using static library?

Programming, for all ages and all languages.
Post Reply
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

Using static library?

Post by junkoi »

Hi,

I am in Linux. I have a small library, and that library comes in 2 forms: dynamic library (libtest.so) and static one (libtest.a). Now I want to write a program using my library. However, I want to use the static library (libtest.a) to compile my program. But when I use this option with gcc: "-ltest -L .", gcc only uses the dynamic library. So when I run my program, it always needs the dynamic library to be put in system paths, which is not available due to not installed there yet.

So which option with gcc can be used so it takes the static library instead?

Many thanks,
J
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Using static library?

Post by AJ »

Hi,

Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.

Cheers,
Adam
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Using static library?

Post by Solar »

Alternatively you could set the environment variable LD_LIBRARY_PATH to whereever your shared library is located, so the linker can find it there even outside of the system search path.
Every good solution is obvious once you've found it.
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

Re: Using static library?

Post by junkoi »

AJ wrote: Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
This doesnt work, and report linking error. The problem is that I want both dynamic and static linking. It is like this: I want dynamic link with library A, and static link with library B (which has libB.a available). So I followed your suggestion, and tried this command: "gcc -lA -lB -L .. -static ..." (libB.a is in ".." directory)

The error report says that there are some "undefined references" to some functions in A. And A has no static library, but only dynamic library in system path (/usr/lib/libA.so)

Any suggestion for this problem?

Many thanks,
J
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Using static library?

Post by AJ »

Hi,

I recently posted a linking question and was given the helpful advice about using --start-group and --end-group flags with ld to denote a group of input libraries/objects. I wonder if you could be looking for the same thing? (I haven't specifically tried what you are doing).

Cheers,
Adam
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Using static library?

Post by quok »

junkoi wrote:
AJ wrote: Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
This doesnt work, and report linking error. The problem is that I want both dynamic and static linking. It is like this: I want dynamic link with library A, and static link with library B (which has libB.a available). So I followed your suggestion, and tried this command: "gcc -lA -lB -L .. -static ..." (libB.a is in ".." directory)

The error report says that there are some "undefined references" to some functions in A. And A has no static library, but only dynamic library in system path (/usr/lib/libA.so)

Any suggestion for this problem?

Many thanks,
J
LD will always try to link with a shared library over a static one, unless -static is specified, in which case it looks for static libs of everything, and will report linker errors if a static library does not exist. Libraries should be specified as the last things on the gcc command line, unless you're using the --start-group and --end-group commands with a separate linker script.

To link with both shared and static libs, drop -static from the command line, and assuming you want to link with the shared library 'foo.so' and the static library bar.a, specify that like this:

Code: Select all

-lfoo -lbar.a
If you do not specify a extension, LD by default looks for a shared lib first and uses that if found, otherwise it will use a static lib. Specifying -shared says to ONLY use shared libs. Likewise -static says to ONLY use static libs. To mix and match during linking, you must specify the extension to use if you want to link against a static lib that also has a shared version available.

That all being said, it may be possible to use -shared and -static at the same time, saying "these libs declared here should be shared libs" and then specifying anything after -static as static libs to link against. I really don't know, I've never tried it and I'm too lazy to look at the man page. :)
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

Re: Using static library?

Post by junkoi »

quok wrote: LD will always try to link with a shared library over a static one, unless -static is specified, in which case it looks for static libs of everything, and will report linker errors if a static library does not exist. Libraries should be specified as the last things on the gcc command line, unless you're using the --start-group and --end-group commands with a separate linker script.

To link with both shared and static libs, drop -static from the command line, and assuming you want to link with the shared library 'foo.so' and the static library bar.a, specify that like this:

Code: Select all

-lfoo -lbar.a
If you do not specify a extension, LD by default looks for a shared lib first and uses that if found, otherwise it will use a static lib. Specifying -shared says to ONLY use shared libs. Likewise -static says to ONLY use static libs. To mix and match during linking, you must specify the extension to use if you want to link against a static lib that also has a shared version available.
This is really intersting, but unfortunately it doesnt work for me.
I have 2 libraries: A (file libA.so) and B (file libB.a). So following your suggestion, I tried both below commands:

1. gcc -main.c -o main -lA -lB.a
2. gcc -main.c -o main -lA -llibB.a

(libB.a is put in the same directory with main.c)

Both report error like: "Cannot find -lB.a" and "Cannot find -llibB.a"

Any idea?

Many thanks,
j
javior
Posts: 1
Joined: Sun Nov 25, 2007 6:09 pm

Re: Using static library?

Post by javior »

First post and I hope this will help.

gcc -c main.c -o main.o
gcc -o main main.o libB.a -lA
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

Re: Using static library?

Post by junkoi »

javior wrote:First post and I hope this will help.

gcc -c main.c -o main.o
gcc -o main main.o libB.a -lA
This simple solution solved the problem for me. That makes me feel really stupid now!

Thanks!
J
Post Reply