0

我想将组件“fsm”链接到我的可执行文件(main.cpp)中。“fsm”依赖于“taskmap”。所以我试图弄清楚如何配置 cmake 以正确方式识别我的库,但配置总是失败并出现以下错误:

错误(配置时产生):

[cmake] CMake Error at components/taskmap/CMakeLists.txt:9 (target_link_libraries):
[cmake]   Attempt to add link library "taskmap" to target "sample_project" which is
[cmake]   not built in this directory.
[cmake] 
[cmake]   This is allowed only when policy CMP0079 is set to NEW.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:8 (include)
[cmake] 
[cmake] 
[cmake] CMake Error at components/fsm/CMakeLists.txt:8 (target_link_libraries):
[cmake]   Attempt to add link library "fsm" to target "sample_project" which is not
[cmake]   built in this directory.
[cmake] 
[cmake]   This is allowed only when policy CMP0079 is set to NEW.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:9 (include)

我的目录结构:

project
├───CMakeLists.txt
├───components
│   ├───fsm
│   │   ├───fsm.cpp
│   │   ├───CMakeLists.txt
│   │   └───include
│   │       └───fsm.hpp
│   └───taskmap
│       ├───CMakeLists.txt
│       └───include
│           └───taskmap.hpp
└───main
    ├───main.cpp
    └───CMakeLists.txt

CMakeLists.txt(顶层):

cmake_minimum_required(VERSION 3.0.0)
project(sample_project VERSION 0.1.0)

include(CTest)
enable_testing()

add_subdirectory(main)
include(components/taskmap/CMakeLists.txt)
include(components/fsm/CMakeLists.txt)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

主/CMakeLists.txt

add_executable(sample_project main.cpp)

组件/fsm/CMakeLists.txt

add_library(
    fsm
    fsm.cpp
    include/fsm.h
)

target_include_directories(fsm PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(sample_project PRIVATE fsm)

组件/任务图/CMakeLists.txt

add_library(
    taskmap
    include/taskmap.h
)

set_target_properties(taskmap PROPERTIES LINKER_LANGUAGE CXX)

target_include_directories(taskmap PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(sample_project PRIVATE taskmap)

我真的不能从这个错误中得到很多,并做了一些研究:

CMake:尝试将链接库添加到未在此目录中构建的目标

这个人试图在顶层 CMakeLists.txt 中使用 target_link_libraries(),即使可执行文件被添加到子 CMakeLists.txt 中。显然 target_link_libraries() 需要从指定目标的 CMakeLists.txt 中调用(除非目录范围保持不变,见下文)。

在子目录中调用 target_link_libraries 的替代方法

此人试图使用 target_link_libraries() 与未在与新创建的目标相同的目录中指定的“链接到”目标(因为输入了新的子目录范围)。我认为这与我的问题有些相关,所以我改用 include() 指令而不是 add_subdirectory(),但错误保持不变

所以到目前为止没有任何帮助。关于我做错了什么的任何线索?

4

0 回答 0