gpt4 book ai didi

c++ - CMake:将依赖项添加到 IMPORTED 库

转载 作者:可可西里 更新时间:2023-11-01 17:44:48 27 4
gpt4 key购买 nike

我有一个供应商提供的库存档,我已将其导入到我的项目中:

add_library(
lib_foo
STATIC
IMPORTED GLOBAL
)

set_target_properties(
lib_foo
PROPERTIES IMPORTED_LOCATION
"${CMAKE_CURRENT_LIST_DIR}/vendor/foo.a"
)

set_target_properties(
lib_foo
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${CMAKE_CURRENT_LIST_DIR}/vendor"
)

当我尝试使用此库链接应用程序时,出现undefined reference to 'pthread_atfork' 链接器错误:

/usr/lib/libtcmalloc_minimal.a(libtcmalloc_minimal_internal_la-static_vars.o):
In function `SetupAtForkLocksHandler':
/tmp/gperftools-2.4/src/static_vars.cc:119:
undefined reference to `pthread_atfork'
../vendor/foo.a(platformLib.o): In function `foo::Thread::Impl::join()':

因此 vendor/foo.a 依赖于 pthread

我尝试了 target_link_libraries(lib_foo pthread) 但它不起作用,因为 lib_foo 是一个IMPORTED 目标,而不是一个内置目标

CMake Error at libfoo/CMakeLists.txt:41 (target_link_libraries):
Attempt to add link library "pthread" to target "lib_foo"
which is not built in this directory.

问题:

如何将 pthread 链接到 lib_foo,或者指定依赖于 lib_foo 的目标也依赖于 pthread ?

最佳答案

IMPORTED_LINK_INTERFACE_LIBRARIES:

您可以设置一个额外的目标属性,IMPORTED_LINK_INTERFACE_LIBRARIES

Transitive link interface of an IMPORTED target.

Set this to the list of libraries whose interface is included when an IMPORTED library target is linked to another target.

The libraries will be included on the link line for the target.

Unlike the LINK_INTERFACE_LIBRARIES property, this property applies to all imported target types, including STATIC libraries.

set_target_properties(lib_foo 
PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES
pthread
)

-pthread 编译器标志:

但是,在这种特殊情况中,pthread 链接问题,可以通过将-pthread 添加到您的编译器标志

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )

来自 man gcc:

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

它会导致文件用-D_REENTRANT 编译,并用-lpthread 链接。在其他平台上,这可能会有所不同。使用 -pthread 以获得大多数可移植性。

See this question for more information

关于c++ - CMake:将依赖项添加到 IMPORTED 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com