'CMake Error: Attempt to add link library to target which is not built in this directory (only when policy CMP0079 is set to NEW)
I want to link the component "fsm" into my executable (main.cpp). "fsm" is dependent on "taskmap". So I was trying to figure out how to configure cmake to recognise my libraries the correct way, but the configuration always fails with the following errors:
Errors (yielded while configuring):
[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)
My directory structure:
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 (toplevel):
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)
main/CMakeLists.txt:
add_executable(sample_project main.cpp)
components/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)
components/taskmap/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)
I can't really make a lot out of this error and have done some research:
CMake: Attempted to add link library to target which is not built in this directory
This person tried to target_link_libraries() in the toplevel CMakeLists.txt, even though the executables are added in a subordered CMakeLists.txt. Apparently target_link_libraries() needs to be called from the CMakeLists.txt where the target is specified (except if the directory scope remains the same, see below).
Alternative to calling target_link_libraries in subdirectory
This person attempted to use target_link_libraries() with the "link to"-target not specified in the same directory as the newly created target (because a new subdirectory scope was entered). I think this is somewhat related to my problem, so I switched to use include() directives instead of add_subdirectory(), but the error stays the same.
So nothing helped so far. Any clues on what I'm doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
