I am writing a header-only library - which I believe is best done using an INTERFACE target (there is nothing to be built after all). The library does have some dependencies, however, which I’d like to be visible from the generated target. Let’s assume the dependency I have is
boost::system. I have the following CMakeLists.txt file for the library:
cmake_minimum_required(VERSION 3.13) project(cmaketest) find_package(Boost 1.48 COMPONENTS system REQUIRED) add_library(testlibrary INTERFACE) target_link_libraries(testlibrary INTERFACE boost::system) add_subdirectory(test)
As you might have guessed, the
test directory contains tests for the library, which contains the following CMakeLists.txt
find_package(Boost 1.48 COMPONENTS system REQUIRED) add_executable(test main.cpp) target_link_libraries(test testlibrary)
CMake is unhappy about this, and presents me the following error message:
CMake Error at test/CMakeLists.txt:3 (add_executable): Target "test" links to target "boost::system" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
This error happens after CMake reports that it found boost twice (once for each CMakeLists.txt). So the
find_package succeeds, yet it still fails because the target was not found.
What am I doing wrong here?