CMake导入的目标在配置时找到但生成了build.make说target-NOTFOUND [英] CMake imported target found when configuring but generated build.make says target-NOTFOUND

查看:176
本文介绍了CMake导入的目标在配置时找到但生成了build.make说target-NOTFOUND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的共享库 libfool2.so ,其中安装了标头 fool2.h ,它们不是来自CMake项目.我的项目 my_temp1 依赖于 fool2 ,因此我编写了一个 FindFool2.cmake 来创建导入的目标:

I have a simple shared library libfool2.so with installed header fool2.h which are not from a CMake project. My project my_temp1 depends on fool2 so I write a FindFool2.cmake to make an imported target:

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

my_temp1 项目的 CMakeLists.txt 是:

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 ?

The command $ cmake -H. -B_builds -DCMAKE_INSTALL_PREFIX=../__install finds fool2 because find_* commands searches in the CMAKE_INSTALL_PREFIX as well. But why is there weird output my_temp1: Fool2::Fool2-NOTFOUND in build.make?

CMake版本为3.11.3

CMake version is 3.11.3

推荐答案

对于 IMPORTED 库目标值 -NOTFOUND 对应于缺少的

For IMPORTED library target value -NOTFOUND corresponds to absent IMPORTED_LOCATION property, corresponded to the library's path. You need to set that property for correctly work with IMPORTED target.

如果要将CMake目标用作占位符只是为了与其他库链接,请改用 INTERFACE 库目标:此类库目标没有库位置.

If you want CMake target to be a placeholder just for link with other libraries, use INTERFACE library target instead: such library target doesn't have library location.

这篇关于CMake导入的目标在配置时找到但生成了build.make说target-NOTFOUND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆