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
Using static library?
Re: Using static library?
Hi,
Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
Cheers,
Adam
Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
Cheers,
Adam
Re: Using static library?
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.
Re: Using static 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)AJ wrote: Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
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?
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
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?
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.junkoi wrote: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)AJ wrote: Add -static to the GCC command line. Otherwise, GCC will always preferentially select the dynamic library.
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
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
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?
This is really intersting, but unfortunately it doesnt work for me.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: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.Code: Select all
-lfoo -lbar.a
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?
First post and I hope this will help.
gcc -c main.c -o main.o
gcc -o main main.o libB.a -lA
gcc -c main.c -o main.o
gcc -o main main.o libB.a -lA
Re: Using static library?
This simple solution solved the problem for me. That makes me feel really stupid now!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
Thanks!
J