Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ linking static and non-static libraries at the same time

I have a makefile project in which I include a few different libraries. One of them is the boost library which I statically link in order to make my program portable. This is how my makefile command looks like:

g++ -O0 -g test.cpp testObject.o -pthread -I/home/user/devel/lmx-sdk-4.7.1/include/ -L/home/user/devel/lmx-sdk-4.7.1/linux_x64 -llmxclient -lrt -ldl -lboost_filesystem  -lboost_system -static -static-libgcc -o $@

I have also linked lmx-sdk library to my project in order to use the licensing functionality; however, it seems to be that lmx-sdk doesn't seem to like static link as it gives an error "Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking".

How can I make it possible to link some libraries statically and the other ones dynamically ?

Thanks in advance

P.S. I have checked some of similar topics and tried a few methods which didn't work out for me.

like image 836
serhatg Avatar asked Sep 09 '14 12:09

serhatg


People also ask

Can a exe link to DLL and static library at the same time?

Yes, the Core and Utils code will be duplicated.

Can you link a static library to a dynamic library?

When you want to “link a static library with dynamic library”, you really want to include the symbols defined in the static library as a part of the dynamic library, so that the run-time linker gets the symbols when it is loading the dynamic library.

What is the disadvantage of static linking?

The major disadvantages of static linking are increases in the memory required to run an executable, network bandwidth to transfer it, and disk space to store it.

Why might you choose to link your program statically rather than use a shared library version?

Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.


1 Answers

Using -Wl,-Bdynamic and -Wl,-Bstatic instead of just using -Bdynamic and -Bstatic solved the problem.

The full link line looks like this now:

g++ -O0 -g test.cpp testObject.o -pthread -Bdynamic -I/home/user/devel/lmx-sdk-4.7.1/include/ -L/home/user/devel/lmx-sdk-4.7.1/linux_x64 -llmxclient -lrt -ldl -Wl,-Bstatic -lboost_filesystem -lboost_system -o $@

like image 80
serhatg Avatar answered Sep 30 '22 06:09

serhatg