cmake添加外部库

时间:2018-04-05 10:59:44

标签: c++ cmake

我想向目标添加依赖项。此依赖项是预编译的lib。我希望能够通过在add_dep(libName)中指定CMakeLists.txt这样的名称来获得外部依赖关系。 add_dep函数应该可以重用于不同的库。

我无法找到任何方法,only for libs which are compiled during build

how would I pass the target name in a generic form here?

would be fine, but the "consumer" shouldn't have to add inc/lib dirs etc

1 个答案:

答案 0 :(得分:1)

基于Some programmer dude

在外部库CMakeList.txt的目录中:

find_library(lib libName dir/to)
add_library(libName SHARED IMPORTED GLOBAL) # GLOBAL -> if outside src tree
set_property(TARGET libName PROPERTY IMPORTED_LOCATION ${lib})
set_property(TARGET libName APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/include)

在" root" CMakeList.txt

add_subdirectory("$ENV{COMMON_ROOT}/libs/libName" "$ENV{COMMON_ROOT}/libs/libName") # usage for out of src ref

target_link_libraries(target libName)添加到需要它的CMakeLists.txt