CMake:将依赖项添加到导入的库 [英] CMake: add dependency to IMPORTED library

查看:482
本文介绍了CMake:将依赖项添加到导入的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个供应商提供的库归档文件,已导入到我的项目中:

I have a vendor supplied library archive which I have imported into my project:

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

当我尝试使用此库链接应用程序时,得到对'pthread_atfork'的未定义引用链接器错误:

When I try to link an application using this library, I get undefined reference to 'pthread_atfork' linker errors:

/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()':

因此供应商/foo.a 依赖于 pthread

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

I tried target_link_libraries(lib_foo pthread) but that doesn't work because lib_foo is an IMPORTED target, rather than a built target


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有依赖性吗?

Question:

How do I link pthread to lib_foo, or specify that targets with a dependency on lib_foo also have a dependency on pthread?

推荐答案

IMPORTED_LINK_INTERFACE_LIBRARIES :



您可以设置其他目标属性, IMPORTED_LINK_INTERFACE_LIBRARIES


IMPORTED的传递链接接口目标。

将此字段设置为当导入
库目标链接到另一个目标。

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.

LINK_INTERFACE_LIBRARIES 属性不同,此属性适用于所有导入的目标类型,包括 STATIC 库。

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 到您的编译器标志

-pthread Compiler Flag:

However, in this particular case, pthread linking issues, the problem would likely be solved by adding -pthread to your compiler flags

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

来自 man gcc


-pthread 添加了对pthreads库中多线程支持。此选项同时为预处理器和链接器设置标志。

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

它使文件使用 -D_REENTRANT ,并与- lpthread 链接。在其他平台上,这可能有所不同。使用 -pthread 具有最大的可移植性。

It causes files to be compiled with -D_REENTRANT, and linked with -lpthread. On other platforms, this could differ. Use -pthread for most portability.

请参阅此问题以获取更多信息

这篇关于CMake:将依赖项添加到导入的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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