如何链接到PIC版本的库的依赖在CMake? [英] How to link against PIC version of a library's dependency in CMake?

查看:474
本文介绍了如何链接到PIC版本的库的依赖在CMake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake:

我们对 cpp-netlib

我们从源代码构建它是我们构建系统的一部分。

We build it from source as part of our build system.

cpp-netlib / CMakeLists.txt

cpp-netlib/CMakeLists.txt:

add_library(cpp-netlib
    STATIC
    ${SRCS})


$ b b

对于一个特定的用例,我们必须在共享库中使用它,所以我创建了一个第二个库 -fPIC enabled:

add_library(cpp-netlib_pic
    STATIC
    ${SRCS})
set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE)



在我的共享库目标中,我链接到 libcpp-netlib_pic .a

foo / CMakeLists.txt

foo/CMakeLists.txt:

add_library(foo
    SHARED
    ${SRCS})
target_link_libraries(foo cpp-netlib_pic)



我收到一个链接器错误,因为 cpp-netlib 尝试链接 boost_network的非pic版本

/usr/bin/ld: ../third_party/cpp-netlib/libcpp-netlib_pic.a(client.cpp.o): \
    relocation R_X86_64_32 against `_ZTVN5boost7network4http4impl15normal_delegateE' \
    can not be used when making a shared object; recompile with -fPIC

解析名称:

$ c++filt _ZTVN5boost7network4http4impl15normal_delegateE
vtable for boost::network::http::impl::normal_delegate






Boost Build

这是将现有构建系统从boost-build迁移到CMake的一部分。

This is all part of migrating our existing build system from boost-build to CMake.

boost-build Jamfiles工作正常。

The boost-build Jamfiles work fine.

Jamroot

Jamroot:

variant pic : release : <cxxflags>-fPIC ;

cpp-netlib / Jamfile

cpp-netlib/Jamfile:

lib cpp-netlib 
: [ glob src/*.cpp ]
;

foo / Jamfile

foo/Jamfile:

shared-lib foo
: [ glob *.cpp ]
  /ext/cpp-netlib//cpp-netlib/<variant>pic
: <link>shared
  <cxxflags>-fPIC
;

请注意,在任何地方都没有提及 boost :: network ,虽然在<$ c $中有一个子文件夹c> cpp-netlib / boost / library ,但仅包含标头。

Note there is no mention of boost::network anywhere, although there is a subfolder in cpp-netlib/boost/library, but it contains headers only.

strong>问题:

Question:

如何告诉CMake cpp-netlib_pic 需要使用pic版本 boost_network

How do I tell CMake that cpp-netlib_pic needs to use the pic version of boost_network?

推荐答案

正确的语法

而不是指定属性:

set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE)

您必须将其切换 ON ::

You have to turn it ON:

set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE ON)

这篇关于如何链接到PIC版本的库的依赖在CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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