我很难理解CMake的导入库。这是我的测试结构:
├── binary
│ ├── CMakeLists.txt
│ └── main.cc
├── library
│ ├── CMakeLists.txt
│ ├── lib.cc
│ └── lib.hh
图书馆的CMakeLists.txt如下所示:
cmake_minimum_required(VERSION 2.8.1)
project(Library)
add_library(library lib.cc)
target_include_directories(library PUBLIC ${CMAKE_CURRENT_LIST_DIR})
二进制文件看起来像这样:
cmake_minimum_required(VERSION 2.8.1)
project(binary)
add_executable(binary main.cc)
find_library(il_default library PATHS "${CMAKE_CURRENT_LIST_DIR}/../library/build/")
add_library(Imported::Library UNKNOWN IMPORTED GLOBAL)
set_property(TARGET Imported::Library PROPERTY IMPORTED_LOCATION ${il_default})
set_property(TARGET Imported::Library PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/../library")
target_link_libraries(binary Imported::Library)
构建命令:
cmake -Blibrary/build -Hlibrary && cmake --build library/build
cmake -Bbinary/build -Hbinary && cmake --build binary/build
这会产生以下错误:
CMake Error at CMakeLists.txt:13 (target_link_libraries):
Target "Imported::Library" of type UNKNOWN_LIBRARY may not be linked into
another target. One may link only to STATIC or SHARED libraries, or to
executables with the ENABLE_EXPORTS property set.
从 add_library 命令中删除 GLOBAL 参数,或用 STATIC 替换 UNKNOWN 解决了这个问题。
我的问题是“为什么?”。为什么我可以在当前目录中使用未知的导入库但不能使其成为全局?
CMake版本是3.6.0