我有一个供应商提供的库存档,我已导入到我的项目中:
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"
)
Run Code Online (Sandbox Code Playgroud)
当我尝试使用此库链接应用程序时,我收到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()':
Run Code Online (Sandbox Code Playgroud)
所以vendor/foo.a依赖于pthread.
我尝试target_link_libraries(lib_foo pthread)但是这不起作用,因为lib_foo它是IMPORTED目标,而不是构建目标
Run Code Online (Sandbox Code Playgroud)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也对并行线程的依赖?
您可以设置其他目标媒体资源IMPORTED_LINK_INTERFACE_LIBRARIES
IMPORTED目标的传递链接接口.将
IMPORTED库目标链接到另一个目标时,将其设置为包含其接口的库列表 .这些库将包含在目标的链接行中.
与
LINK_INTERFACE_LIBRARIES属性不同,此属性适用于所有导入的目标类型,包括STATIC库.
set_target_properties(lib_foo
PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES
pthread
)
Run Code Online (Sandbox Code Playgroud)
但是,在这种特殊情况下,pthread链接问题,可能会通过添加-pthread到编译器标志来解决问题
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
Run Code Online (Sandbox Code Playgroud)
来自man gcc:
-pthread使用pthreads库添加对多线程的支持.此选项为预处理器和链接器设置标志.
它导致文件-D_REENTRANT与 - 编译,并与 - 链接lpthread.在其他平台上,这可能会有所不同.使用-pthread于大多数便携性.
| 归档时间: |
|
| 查看次数: |
4198 次 |
| 最近记录: |