从导入的库传递上需要其他导入库的接口

时间:2016-07-27 14:45:49

标签: cmake

CMake 3.5

我有一个现有的外部库,我在IMPORTED中创建了一个CMakeLists.txt库:

find_path(
    FOO_INCLUDE_DIR NAMES foo.h
    PATHS "${FOO_ROOT}"
    NO_DEFAULT_PATH
    PATH_SUFFIXES include/foo
)
find_library(
    FOO_LIBRARY NAMES foo
    PATHS "${FOO_ROOT}"
    PATH_SUFFIXES lib
)
mark_as_advanced(FOO_INCLUDE_DIR FOO_LIBRARY)
find_package_handle_standard_args(
    FOO REQUIRED_VARS
    FOO_INCLUDE_DIR
    FOO_LIBRARY
)
add_library(Foo::Foo SHARED IMPORTED)
set_property(TARGET Foo::Foo
    PROPERTY INTERFACE_INCLUDE_DIRECTORIES
    "${FOO_INCLUDE_DIR}" "${BAR_INCLUDE_DIR}"
    "${Boost_INCLUDE_DIRS}" "${BAZ_INCLUDE_DIR}"
)
set_property(TARGET Foo::Foo
    PROPERTY IMPORTED_LOCATION
    "${FOO_LIBRARY}"
)
set_property(TARGET Foo::Foo
    PROPERTY INTERFACE_LINK_LIBRARIES
    "${Boost_LIBRARIES}" "${BAR_LIBRARY}" "${BAZ_LIBRARIES}"
)

与此特定库链接的程序需要线程支持。为某些东西添加线程支持非常简单:

set(THREADS_PREFER_PTHREAD_FLAG on)
include(FindThreads)
...
target_link_libraries(something PUBLIC Threads::Threads)

我希望任何与Foo::Foo相关联的内容自动包含Threads::Threads。但是您无法在target_link_libraries()库上使用IMPORTED。那么我如何从Threads::Threads过渡性地要求Foo::Foo

我设法通过执行以下操作来解决此问题,但这取决于我检查FindThreadsThreads::Threads设置的属性。还有更好的方法吗?

set_property(TARGET Foo::Foo
    PROPERTY INTERFACE_LINK_LIBRARIES
    "${Boost_LIBRARIES}" "${BAR_LIBRARY}" "${BAZ_LIBRARIES}"
    $<TARGET_PROPERTY:Threads::Threads,INTERFACE_LINK_LIBRARIES>
)
set_property(TARGET Foo::Foo
    PROPERTY INTERFACE_COMPILE_OPTIONS
    $<TARGET_PROPERTY:Threads::Threads,INTERFACE_COMPILE_OPTIONS>
)

1 个答案:

答案 0 :(得分:0)

It turns out the answer was quite simple. I simple needed to add Threads::Threads to the INTERFACE_LINK_LIBRARIES property of Foo::Foo. I have no idea why I had determined that would not work the first time around.