CMake:针对libgcc和libstdc ++静态链接到共享库中 [英] CMake: Linking statically against libgcc and libstdc++ into a shared library

查看:842
本文介绍了CMake:针对libgcc和libstdc ++静态链接到共享库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我在Ubuntu 16.04安装上使用CMake和GCC4.9将glibcc / glibc ++链接到共享库时遇到困难。

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

其他条件:

加载共享库会给我相信在Red Hat生产环境(我将其复制到的环境)中,因为它使用了不同的libstc ++版本(错误:找不到GLIBCXX_3_4_20)。我没有sudo权限,无法升级计算机。

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.

我来自博客,此帖子 ,我尝试使用以下方法对libgcc和libgc ++进行静态链接:

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")

,然后再次使用

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

但这不起作用。这个CMake脚本的工作是什么:

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++)

这一定是错误的方法,据我所知-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...

问题
我如何正确地静态链接-libgcc和-libstdc ++?

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

谢谢!

推荐答案

是的, target_link_libraries 是设置链接器标记的正确方法或链接器选项。

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

target_link_libraries 的文档:


指定在链接给定目标时要使用的库或标志

以-开头但不以-l或-framework开头的项目名称被视为
链接器标志。

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

< a href = https://cmake.org/cmake/help/v3.0/command/target_link_libraries.html rel = noreferrer> https://cmake.org/cmake/help/v3.0/command/ target_link_libraries.html (不是原始重点)

这篇关于CMake:针对libgcc和libstdc ++静态链接到共享库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆