2

I am writing a program using Qt Creator, on Linux. It uses a library, for which I have both the .so and the .a equivalents in the /usr/lib directory, for example:

/usr/lib/libuseme.a

/usr/lib/libuseme.so

From what I understand, if I link to the .so, it will be linked dynamically, but if I link to the .a it will be linked statically.

In this case, I want to link statically, to give me a better chance of distributing my program in a self-contained way.

But the -l option to the linker only allows the library name, not including the lib or the extension to be specified - e.g.

-luseme

So how do I indicate that I want it to link (statically) to the .a, not dynamically to the .so?

2 Answers2

1

So how do I indicate that I want it to link (statically) to the .a, not dynamically to the .so?

Two ways:

  • Link using the full library path /usr/lib/libuseme.a instead of -luseme
  • Tell the linker that you want archive copy: -Wl,-Bstatic -luseme -Wl,-Bdynamic
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • you can also split the building process in 2 main parts, you compile an object first without linking anything, this is usually accomplished with the the `-c` flag and then you link objects and libraries in a second step as you wish. this is usually a better option when you want a more articulated linking phase and it's also relatively easy to set-up and handle, a simple makefile or any other building tool will do the job. – user2485710 Jul 13 '13 at 05:08
0

You may use -static to force static linking.

However, if you don't want to statically link with every library, you can add the paths to the .a files of the libraries for static linking to the command with the rest of the files.

S..
  • 1