AxeEffect
AxeEffect

Reputation: 7501

Using Linux C++ shared and static libraries in a same program

How can I use both C++ shared and static libraries in a same Linux program?

When managing with g++, I've tried to arrange -static command ahead and behind the libraries I don't want to link statically, but with no results. ld through g++ complains about where are the .a files of the shared libraries (cannot find -lwhatever error).

EDIT: the problem isn't the cannot find -lwhatever error, since it just happens because there isn't static version of the specified library. What I'm trying to do is to specify which libraries are to be statically linked and which are to be dynamically.

Upvotes: 2

Views: 9908

Answers (2)

Assuming a static libfoo.a and a dynamic libbar.so you could use

g++ -o prog main.o other.o -Wl,-Bstatic -lfoo -Wl,-Bdynamic -lbar

You should avoid calling functions in a static library from a dynamic one; this would be ugly.

hint

The -Wl options to g++ are used to pass arguments to the ld linker invoked by g++.

You may want to use g++ -v to understand how g++ invokes ld, and you could also use g++ -v -Wl,--verbose to also ask ld to be verbose.

Upvotes: 6

jpyllman
jpyllman

Reputation: 305

You do not need to specify -static or -dynamic. The format of the file you link with specify if it is a shared or a static link. You just need to choose the correct file to link with.

Upvotes: 2

Related Questions