質問

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?

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top