CMake: binding statically to libgcc and libstdC ++ to a shared library

Problem:

I'm having difficulty linking glibcc / glibc ++ to a shared library using CMake and GCC4.9 on my Ubuntu 16.04 installation.

Additional terms:

Downloading the shared library poses a problem in the Red Hat production environment (where I copy it), I suppose, because it uses a different version of libstc ++ (error: GLIBCXX_3_4_20 not found). I do not have sudo permissions and cannot update the machine.

As I understood from this blog, in this post I tried to link static links with libgcc and libgc ++ using:

set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static") 

and using again

 set(CMAKE_SHARED_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static") 

But that does not work. What this CMake script works:

 add_library(myLib SHARED ${SOURCE_FILES}) set(CMAKE_EXE_LINKER_FLAGS " -static") target_link_libraries(myLib -static-libgcc -static-libstdc++) 

This must be the wrong way to do this, as far as I know, -static -libgcc and -static-libstdC ++ are linker options, not libraries ...

Question : How to link statically with -libgcc and -libstdC ++?

Thanks in advance!

+15
c ++ gcc g ++ cmake static-linking
source share
1 answer

Yes, target_link_libraries is the right way to set linker flags or linker options.

Documentation target_link_libraries :

Specify libraries or flags to use when linking a given target.

Product names beginning with - but not -l or -framework are treated as linker flags.

https://cmake.org/cmake/help/v3.0/command/target_link_libraries.html (emphasis not on the original)

+12
source share

All Articles