Why must I install an header only lib which I only need at build time for my own library?
Claus-iMac:example clausklein$ cmake -B build -S . -G Ninja
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Downloading CPM.cmake to /Users/clausklein/cmake/example/build/cmake/CPM_0.28.2.cmake
-- Version: 7.1.3
-- Build type:
-- CXX_STANDARD: 17
# ...
-- Performing Test FMT_HAS_VARIANT
-- Performing Test FMT_HAS_VARIANT - Success
-- Required features: cxx_variadic_templates
-- Configuring done
CMake Error: install(EXPORT "GreeterTargets" ...) includes target "Greeter" which requires target "fmt-header-only" that is not in any export set.
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
Claus-iMac:example clausklein$
cmake_minimum_required(VERSION 3.14...3.19)
# ---- Project ----
# Note: update this to your new project's name and version
project(
Greeter
VERSION 1.0
LANGUAGES CXX
)
# ---- Project settings ----
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
# ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info
include(../CPM.cmake)
# CPMAddPackage(NAME PackageProject.cmake GITHUB_REPOSITORY TheLartians/PackageProject.cmake VERSION
# 1.4)
include(../PackageProject.cmake)
# PackageProject.cmake will be used to make our target installable
option(USE_FETCH_CONTENT "to show the problem is not caused by CPM" ON)
# XXX find_package(fmt 7.1)
if(NOT TARGET fmt::fmt-header-only)
# NOTE: If fmt is not imported, we need to install it! CK
# XXX option(FMT_INSTALL "" ON)
option(FMT_INSTALL "" OFF)
# to prevent CMake Error: install(EXPORT # "GreeterTargets" ...) includes target "Greeter" which
# requires target "fmt" that is not in any export set.
if(USE_FETCH_CONTENT)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 7.1.3
)
FetchContent_MakeAvailable(fmt)
else()
CPMAddPackage(
NAME fmt
GIT_TAG 7.1.3
GITHUB_REPOSITORY fmtlib/fmt
)
endif()
endif()
# ---- Create library ----
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
# target:
add_library(Greeter)
add_library(${PROJECT_NAME}::Greeter ALIAS Greeter)
target_compile_features(Greeter PUBLIC cxx_std_17)
target_sources(Greeter PRIVATE source/Greeter.cpp include/greeter/greeter.h)
# Link dependencies (if required)
target_link_libraries(Greeter PRIVATE fmt::fmt-header-only)
target_include_directories(
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)
# ---- Create an installable target ----
# this allows users to install and find the library via `find_package()`.
# the location where the project's version header will be placed should match the project's regular
# header paths
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
DEPENDENCIES "" # optional list of dependencies
)