Closed Life4gal closed 3 years ago
OPTIONS
parameter, as otherwise the package might ignore them. cxxopts
isn't installable by default. You should also provide the CXXOPTS_ENABLE_INSTALL
parameter. We should probably add it in the starter as well.CPMAddPackage(
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 2.2.1
OPTIONS
"CXXOPTS_BUILD_EXAMPLES OFF"
"CXXOPTS_BUILD_TESTS OFF"
"CXXOPTS_ENABLE_INSTALL ON"
)
- Be sure to define the option before adding the package as cache variables, or simply provide them to CPM.cmake in the
OPTIONS
parameter, as otherwise the package might ignore them.- I think the error is due to the fact that
cxxopts
isn't installable by default. You should also provide theCXXOPTS_ENABLE_INSTALL
parameter. We should probably add it in the starter as well.- Also prefer git tags as version identifiers, as otherwise the build won't be reproducible in the future.
CPMAddPackage( GITHUB_REPOSITORY jarro2783/cxxopts VERSION 2.2.1 OPTIONS "CXXOPTS_BUILD_EXAMPLES OFF" "CXXOPTS_BUILD_TESTS OFF" "CXXOPTS_ENABLE_INSTALL ON" )
Yes this eliminates the error, but can you explain to me why it doesn't work of my code? What puzzles me most is why my code still runs normally despite the error reported by CMake.
You're adding cxxopts, which is not configured for installation, to a library that is. As you need to be able install all dependencies, CMake gives an error. By configuring cxxopts for installation this error is resolved.
You're adding cxxopts, which is not configured for installation, to a library that is. As you need to be able install all dependencies, CMake gives an error. By configuring cxxopts for installation this error is resolved.
Could you please tell me what should I do if I want to install nlohmann/json(or nlohmann/json's FetchContent)(and others library) in this way? Because it seems that not all libraries have a "FOO_ENABLE_INSTALL YES" option, sorry, I don’t know much about this :)
The example not suitable for me, as mentioned earlier, my top level project is not built by itself.
Oh yeah, seems we missed that in the example. You'd have to set JSON_Install
to YES
in this case. Unfortunately, as far as I know, there is no great way of doing this besides looking at the project's CMakeLists and finding out how to enable the libraries installation from there.
Oh yeah, seems we missed that in the example. You'd have to set
JSON_Install
toYES
in this case. Unfortunately, as far as I know, there is no great way of doing this besides looking at the project's CMakeLists and finding out how to enable the libraries installation from there.
# fmt always work well :)
CPMAddPackage(
NAME fmt
GIT_TAG master
GITHUB_REPOSITORY fmtlib/fmt
# create an installable target, this is necessary
# https://github.com/fmtlib/fmt/blob/9cb347b4b2e80fc9fbf57b8621746663c3f870f6/CMakeLists.txt#L67
OPTIONS "FMT_INSTALL YES"
)
CPMAddPackage(
NAME nlohmann_json
VERSION 3.9.1
# the git repo is incredibly large, so we download the archived include directory
URL https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip
URL_HASH SHA256=6bea5877b1541d353bd77bdfbdb2696333ae5ed8f9e8cc22df657192218cad91
# https://github.com/nlohmann/json/blob/823801879ab9a99440b300a02b737c11e806d207/CMakeLists.txt#L34
# why no effect
OPTIONS "JSON_Install YES"
)
# todo: If the following code exists, everything work well, but...
# CMake Error: install(EXPORT "StarterTemplateTargets" ...) includes target "StarterTemplate" which requires target "nlohmann_json" that is not in any export set.
if(nlohmann_json_ADDED)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json SYSTEM INTERFACE ${nlohmann_json_SOURCE_DIR}/include)
endif()
target_link_libraries(
${PROJECT_NAME}
PUBLIC
fmt
nlohmann_json
)
Oh yeah you're creating your own JSON target, that isn't installable. Try replacing the block
CPMAddPackage(
NAME nlohmann_json
VERSION 3.9.1
# the git repo is incredibly large, so we download the archived include directory
URL https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip
URL_HASH SHA256=6bea5877b1541d353bd77bdfbdb2696333ae5ed8f9e8cc22df657192218cad91
# https://github.com/nlohmann/json/blob/823801879ab9a99440b300a02b737c11e806d207/CMakeLists.txt#L34
# why no effect
OPTIONS "JSON_Install YES"
)
# todo: If the following code exists, everything work well, but...
# CMake Error: install(EXPORT "StarterTemplateTargets" ...) includes target "StarterTemplate" which requires target "nlohmann_json" that is not in any export set.
if(nlohmann_json_ADDED)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json SYSTEM INTERFACE ${nlohmann_json_SOURCE_DIR}/include)
endif()
with something like
CPMAddPackage(
NAME nlohmann_json
VERSION 3.9.1
OPTIONS
"JSON_Install ON"
"JSON_BuildTests OFF"
)
Be sure to set the CPM_SOURCE_CACHE
environmental variable (e.g. to ~/.cache/cpm
) to allow a shallow clone and avoid redundant downloads of the repo.
Oh yeah you're creating your own JSON target, that isn't installable. Try replacing the block
CPMAddPackage( NAME nlohmann_json VERSION 3.9.1 # the git repo is incredibly large, so we download the archived include directory URL https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip URL_HASH SHA256=6bea5877b1541d353bd77bdfbdb2696333ae5ed8f9e8cc22df657192218cad91 # https://github.com/nlohmann/json/blob/823801879ab9a99440b300a02b737c11e806d207/CMakeLists.txt#L34 # why no effect OPTIONS "JSON_Install YES" ) # todo: If the following code exists, everything work well, but... # CMake Error: install(EXPORT "StarterTemplateTargets" ...) includes target "StarterTemplate" which requires target "nlohmann_json" that is not in any export set. if(nlohmann_json_ADDED) add_library(nlohmann_json INTERFACE) target_include_directories(nlohmann_json SYSTEM INTERFACE ${nlohmann_json_SOURCE_DIR}/include) endif()
with something like
CPMAddPackage( NAME nlohmann_json VERSION 3.9.1 OPTIONS "JSON_Install ON" "JSON_BuildTests OFF" )
Be sure to set the
CPM_SOURCE_CACHE
environmental variable (e.g. to~/.cache/cpm
) to allow a shallow clone and avoid redundant downloads of the repo.
This can indeed solve the problem, but it will bring a problem that cannot be ignored: the size of the json library is 250MB.
My operating system is KDE (ubuntu20.04) and the IDE I use is CLion. Can you explain what you said [set the CPM_SOURCE_CACHE
], or how to do it, I don’t really understand.
If you add something like export CPM_SOURCE_CACHE=$HOME/.cache/CPM
to your .bash_profile
, CPM.cmake will cache downloads and also use shallow clones. In that case the json library still needs around 170mb, but it will be downloaded only once on your system. See the CPM.cmake docs for more info.
Alternatively, you can download only the header as before, but then it's your responsibility to create an installable target. Another option that could work would be to simply add the include to your main (already installable) target target_include_directories(${PROJECT_NAME} PRIVATE ${nlohmann_json_SOURCE_DIR}/include)
.
I noticed the CMakeList.txt file at the top level of your project, the line 55
Use target_link_libraries to link an external library which add by
I am puzzled why this can be done, as far as I know, this will remind me of an error on my IDE (CLion).
This is the structure of my project:
The content of CMakeList.txt in top level:
The content of CMakeList.txt in exec:
You may have noticed that I have a mark in each of the two files.
At the beginning, I wrote the code that needs cxxopts in the exec folder (the same directory as main.cpp) and the exec folder's structure is similar to the top level (also has include and src folders), I use
everything work well
But soon I discovered that there are some problems with the design of this structure. I need to move the code that uses cxxopts to the include and src folders at the top level. So I tried to delete the cxxopts at MARK2 to
And added the code at MARK1 (they didn't exist at the beginning).
then
After this, I came back here, I use target_link_libraries like you
What I didn’t expect was that the error this time was not
It output
What I didn't expect again is
I don't know what to do.