Prefix PUBLIC_HEADER with CMAKE_CURRENT_SOURCE_DIR
This is required to install the project when using cmake FetchContent, or it will complain about the file json-schema.hpp not found (because it will use a relative path to the project that called FetchContent instead of json-schema-validator path
Could you share an example build? CMAKE_CURRENT_SOURCE_DIR should be implicitly included. It might be a CMake version issue or cmake_policy instead.
Sure, here is a minimal example: CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(Example)
include(FetchContent)
set(JSON_SCHEMA_VALIDATOR_VERSION 2.3.0)
FetchContent_Declare(JSON_SCHEMA_VALIDATOR
URL https://github.com/pboettch/json-schema-validator/archive/refs/tags/${JSON_SCHEMA_VALIDATOR_VERSION}.tar.gz)
FetchContent_MakeAvailable(JSON_SCHEMA_VALIDATOR)
add_library(example main.cpp)
target_link_libraries(example PUBLIC nlohmann_json_schema_validator)
install(TARGETS
example
nlohmann_json
nlohmann_json_schema_validator
EXPORT
exampleConfig
)
install(EXPORT exampleConfig DESTINATION cmake)
main.cpp
#include <nlohmann/json-schema.hpp>
int main()
{
return 0;
}
Run cmake . -B build && make -C build && sudo make -C build install gives the following error:
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib64/libexample.a
-- Installing: /usr/local/lib64/libnlohmann_json_schema_validator.a
CMake Error at cmake_install.cmake:54 (file):
file INSTALL cannot find
"/home/[email protected]/dev/example_fetch_content/nlohmann/json-schema.hpp": No
such file or directory.
I am using cmake 3.26.5 on Rocky Linux 9 (RHEL 9)
Ok I did some debugging. Try this CMakeLists.txt instead:
cmake_minimum_required(VERSION 3.25...3.30)
project(Example)
include(FetchContent)
option(JSON_VALIDATOR_INSTALL "example: Override" ON)
option(JSON_Install "example: Override" ON)
FetchContent_Declare(JSON_SCHEMA_VALIDATOR
GIT_REPOSITORY https://github.com/pboettch/json-schema-validator
GIT_TAG 2.3.0
)
FetchContent_MakeAvailable(JSON_SCHEMA_VALIDATOR)
add_library(example main.cpp)
target_link_libraries(example PRIVATE nlohmann_json_schema_validator)
install(TARGETS
example
EXPORT
exampleConfig
)
install(EXPORT exampleConfig DESTINATION cmake)
Explanation:
- install instructions are already defined in each upstream project. If you really want to install the projects override the options of the projects, i.e. set an
option()before theFetchContentcommands. Beware that it would possibly override any pre-installed versions, particularly theConfig.cmakefiles. - try to link everything as
PRIVATEif you do not need to propagate the#include. If that is the case, you do not really need thenlohmann_json_schema_validatorheader files to be installed - alternatively make sure
exampleisSHAREDlibrary and you can disable the installation for the dependencies
You reminded me of the weird edge-case that when you have a static library foo that depends on bar, then the install fails even though the linkage is PRIVATE. I still don't know why exactly that is the case, but if you would know anyone who might know, would be awesome if you could direct them to: https://github.com/LecrisUT/CMake-Template/issues/15
Edit: I've tried to revisit the problem and made MWE and I am afraid things can get quite messy if you want to support both static and shared library build