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

2

6

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.

Doge answered 9/9, 2014 at 12:1 Comment(0)
D
8

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 $@

Doge answered 11/9, 2014 at 8:53 Comment(2)
Sorry forgot to put it in here. It's corrected now.Doge
there is still a -Bdynamic in your compiler commandSutlej
L
3

You can use -Bstatic to statically link what comes after it, then -Bdynamic to do the opposite. As many times as you need on the command line.

Lightyear answered 9/9, 2014 at 12:9 Comment(3)
thanks for the answer. I have tried it before, but it didn't work out. Now it gives an error from boost library, which is: "error while loading shared libraries: libboost_filesystem.so.1.54.0: cannot open shared object file: No such file or directory" in a system where boost library doesn't exist. When I use -static flag, then I am able to run my program without requiring boost libraries installed, but then I get this other error above.Doge
What was the full link line when you built the one you just described as failing?Lightyear
"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 -Bstatic -lboost_filesystem -lboost_system -static-libgcc -o $@"Doge

© 2022 - 2024 — McMap. All rights reserved.