13

I have both static and dynamic versions of the same library installed on my Linux system:

/usr/lib/libsample.a
/usr/lib/libsample.so

Which one of these libraries will use gcc, if I set argument -lsample ?

gcc xxx.c -lsample

How can I force gcc to use static or dynamic version of the library

Sandro
  • 2,707
  • 2
  • 20
  • 30

1 Answers1

8

http://www.rapidtables.com/code/linux/gcc/gcc-l.htm

You don't have to do anything, dynamic is the default.

However, it has to add a couple of things to the binary to help it, at runtime, to properly use the dynamic library.

Taiki
  • 629
  • 5
  • 13
  • ok, dynamic will be used by default, but how to say compiler to use static library? If I use "-static" it will links only static libraries. Is there any way to link only particular static library? – Sandro Mar 30 '14 at 22:35
  • 1
    http://stackoverflow.com/questions/809794/use-both-static-and-dynamically-linked-libraries-in-gcc – Taiki Mar 31 '14 at 05:20
  • 2
    One way is just directly specify path to `.a` (without any `-llibname` - just path to file, like ordinary `.o`). Other way is `-Wl,-static -llibname -Wl,-Bdynamic` (first command sets linker to use static, third reverts back to dynamic). – keltar Mar 31 '14 at 05:20