配置时找到了 CMake 导入的目标,但生成了 build.make 说 target-NOTFOUND

pte*_*gon 4 cmake

我有一个简单的共享库,libfool2.so其中安装了fool2.h不是来自 CMake 项目的标头。我的项目my_temp1取决于fool2所以我写了一个FindFool2.cmake来制作一个导入的目标:

find_path(Fool2_INCLUDE_DIR fool2.h PATH_SUFFIXES fool2)
find_library(Fool2_LIB fool2)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Fool2
    REQUIRED_VARS Fool2_INCLUDE_DIR Fool2_LIB
)

if(Fool2_FOUND AND NOT TARGET Fool2::Fool2)
    add_library(Fool2::Fool2 SHARED IMPORTED)
    set_target_properties(Fool2::Fool2 PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${Fool2_INCLUDE_DIR}"
        INTERFACE_LINK_LIBRARIES "${Fool2_LIB}"
    )
endif()
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txtmy_temp1项目是:

cmake_minimum_required(VERSION 3.3)
project(my_temp1)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/cmake_modules) 
# FindFool2.cmake is in ${CMAKE_CURRENT_LIST_DIR}/cmake/cmake_modules

find_package(Fool2 REQUIRED)

if (TARGET Fool2::Fool2)
    message(STATUS "target found")
endif()

add_executable(my_temp1 main.cpp)
target_link_libraries(my_temp1 Fool2::Fool2)
Run Code Online (Sandbox Code Playgroud)

现在

$ tree ../__install
../__install/
??? include
?   ??? fool2
?       ??? fool2.h
?       ??? version.h
??? lib
    ??? libfool2.so
$ tree .
.
??? cmake
?   ??? cmake_modules
?       ??? FindFool2.cmake
??? CMakeLists.txt
??? main.cpp
$ cmake -H. -B_builds -DCMAKE_INSTALL_PREFIX=../__install
# some output omitted
-- target found
-- Configuring done
-- Generating done
-- Build files have been written to: /OMITTED/my_temp1/_builds
$ cmake --build _builds
CMakeFiles/my_temp1.dir/build.make:82: *** target pattern contains no '%'.  Stop.
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/my_temp1.dir/all' failed
make[1]: *** [CMakeFiles/my_temp1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
$ head -n 85 _builds/CMakeFiles/my_temp1.dir/build.make | tail -n 10

# External object files for target my_temp1
my_temp1_EXTERNAL_OBJECTS =

my_temp1: CMakeFiles/my_temp1.dir/main.cpp.o
my_temp1: CMakeFiles/my_temp1.dir/build.make
my_temp1: Fool2::Fool2-NOTFOUND
my_temp1: CMakeFiles/my_temp1.dir/link.txt
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/OMITTED/my_temp1/_builds/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking CXX executable my_temp1"
    $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/my_temp1.dir/link.txt --verbose=$(VERBOSE)
Run Code Online (Sandbox Code Playgroud)

该命令会$ cmake -H. -B_builds -DCMAKE_INSTALL_PREFIX=../__install查找,fool2因为 find_* 命令也在 中搜索CMAKE_INSTALL_PREFIX。但是为什么my_temp1: Fool2::Fool2-NOTFOUND在 build.make 中有奇怪的输出?

CMake 版本是 3.11.3

Tsy*_*rev 8

对于IMPORTED库目标值-NOTFOUND对应于缺少IMPORTED_LOCATION属性,对应于库的路径。您需要设置该属性才能正确使用IMPORTED目标。


如果您希望 CMake 目标成为仅用于与其他库链接的占位符,请改用INTERFACE库目标:此类库目标没有库位置。