32

Problem:

I am having difficulties linking glibcc/glibc++ into a shared library using CMake and GCC4.9 on my Ubuntu 16.04 installation.

Additional conditions:

Loading the shared library gives a problem om the Red Hat production environment(where I copy it to), I believe because it uses a different libstc++ version(error: GLIBCXX_3_4_20 not found). I do not have sudo rights and cannot upgrade the machine.

As I derived from this blog, this post, I tried linking static linking against libgcc and libgc++ using:

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

and againg using

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

But that doesn't work. What does work is this CMake script:

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 of doing this, to my knowledge -static-libgcc and -static-libstdc++ are linker options and not libraries...

Question: How do I link statically against -libgcc and -libstdc++ correctly?

Thanks in advance!

0

2 Answers 2

20

Yes, target_link_libraries is a correct way to set linker flags or linker options.

Documentation of target_link_libraries:

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

Item names starting with -, but not -l or -framework, are treated as linker flags.

https://cmake.org/cmake/help/latest/command/target_link_libraries.html (emphasis not in original)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Ok, I should have read the manual better... I think the name target_link_libraries is a bit confusing though...
Guess it also means "target link flags" but yeah...
9

As of cmake 3.13, there is a new cmake function for general linker options:

https://cmake.org/cmake/help/v3.13/command/target_link_options.html

target_link_options(<target> [BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

The appropriate way to specify libraries to be linked is still:

https://cmake.org/cmake/help/v3.13/command/target_link_libraries.html

target_link_libraries(<target>
  <PRIVATE|PUBLIC|INTERFACE> <item>...
  [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)

There are a few different signatures depending on whether or not you want these libraries to be propagated to dependent targets, so be sure to check the docs.

3 Comments

Why couldn't you just show the exact code... would have been much easier..
target_link_options(myLib PRIVATE -static-libgcc -static-libstdc++)
target_link_options(myLib PRIVATE -static-libstdc++) helped to solve error: /usr/bin/ld: : undefined reference to `std::ios_base_library_init()@GLIBCXX_3.4.32'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.