我有一个供应商提供的库存档,我已将其导入到我的项目中:
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()':
So vendor/foo.a
依赖于pthread
.
I tried target_link_libraries(lib_foo pthread)
但这行不通,因为lib_foo
is an 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
to lib_foo
,或指定依赖于的目标lib_foo
还依赖于pthread?
IMPORTED_LINK_INTERFACE_LIBRARIES:
您可以设置一个额外的目标属性,IMPORTED_LINK_INTERFACE_LIBRARIES
传递链路接口IMPORTED
target.
将其设置为当调用时包含其接口的库列表IMPORTED
库目标链接到另一个目标。
这些库将包含在目标的链接行中。
不像LINK_INTERFACE_LIBRARIES
属性,该属性适用于所有导入的目标类型,包括STATIC
图书馆。
set_target_properties(lib_foo
PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES
pthread
)
-pthread 编译器标志:
然而,在这个特殊案例, pthread
链接问题,问题可能会通过添加来解决-pthread
到你的编译器标志
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
From man gcc
:
-pthread
使用 pthreads 库添加对多线程的支持。该选项为预处理器和链接器设置标志。
它会导致文件被编译-D_REENTRANT
,并与 - 连接lpthread
。在其他平台上,这可能有所不同。使用-pthread
对于大多数便携性。
请参阅此问题以获取更多信息
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)