CMake在配置时找到导入的目标但生成的build.make表示目标-NOTFOUND

时间:2018-06-12 15:06:45

标签: 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()

CMakeLists.txt项目的my_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)

现在

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

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

CMake版本是3.11.3

1 个答案:

答案 0 :(得分:3)

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

如果您希望CMake目标只是与其他库链接的占位符,请使用 INTERFACE 库目标:此类库目标没有库位置。