[英]CMake: Linking statically against libgcc and libstdc++ into a shared library
Problem:问题:
I am having difficulties linking glibcc/glibc++ into a shared library using CMake and GCC4.9 on my Ubuntu 16.04 installation.我在 Ubuntu 16.04 安装上使用 CMake 和 GCC4.9 将 glibcc/glibc++ 链接到共享库时遇到困难。
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).加载共享库会在 Red Hat 生产环境(我将其复制到该环境)中出现问题,我相信是因为它使用了不同的 libstc++ 版本(错误:未找到 GLIBCXX_3_4_20)。 I do not have sudo rights and cannot upgrade the machine.我没有 sudo 权限,无法升级机器。
As I derived from this blog, this post , I tried linking static linking against libgcc and libgc++ using:正如我从这篇博客文章中得出的那样,我尝试使用以下方法将静态链接与 libgcc 和 libgc++ 链接起来:
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:什么工作是这样的CMake的脚本:
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...这一定是错误的做法,据我所知 -static-libgcc 和 -static-libstdc++ 是链接器选项而不是库...
Question : How do I link statically against -libgcc and -libstdc++ correctly?问题:如何正确地静态链接 -libgcc 和 -libstdc++?
Thanks in advance!提前致谢!
Yes, target_link_libraries
is a correct way to set linker flags or linker options.是的, target_link_libraries
是设置链接器标志或链接器选项的正确方法。
Documentation of target_link_libraries
: 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.以 - 开头但不以 -l 或 -framework 开头的项目名称被视为链接器标志。
https://cmake.org/cmake/help/v3.0/command/target_link_libraries.html (emphasis not in original) https://cmake.org/cmake/help/v3.0/command/target_link_libraries.html (强调不是原文)
As of cmake 3.13, there is a new cmake function for general linker options:从 cmake 3.13 开始,有一个用于通用链接器选项的新 cmake 函数:
https://cmake.org/cmake/help/v3.13/command/target_link_options.html 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 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.根据您是否希望将这些库传播到相关目标,有几种不同的签名,因此请务必查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.