Page 1 of 1

Using static library?

Posted: Tue Sep 16, 2008 4:04 am
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

Re: Using static library?

Posted: Tue Sep 16, 2008 4:20 am
by AJ
Hi,

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

Cheers,
Adam

Re: Using static library?

Posted: Tue Sep 16, 2008 5:04 am
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.

Re: Using static library?

Posted: Tue Sep 16, 2008 10:38 pm
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

Re: Using static library?

Posted: Wed Sep 17, 2008 1:44 am
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

Re: Using static library?

Posted: Wed Sep 17, 2008 10:30 am
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. :)

Re: Using static library?

Posted: Thu Sep 18, 2008 9:46 am
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

Re: Using static library?

Posted: Fri Sep 19, 2008 5:36 pm
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

Re: Using static library?

Posted: Sat Sep 20, 2008 6:56 am
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