Say I want to create an export configuration for a library with a dependency provided by FetchContent_MakeAvailable, like the following:
Include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.2.1 # or a later release
)
FetchContent_MakeAvailable(fmt)
...
target_link_libraries(${module_name} PRIVATE fmt::fmt)
How should I write my export configuration file? Is it good practice to simply use FetchContent_MakeAvailable also in the config file like this:
@PACKAGE_INIT@
Include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.2.1 # or a later release
)
FetchContent_MakeAvailable(fmt)
include(${CMAKE_CURRENT_LIST_DIR}/mylib-config-version.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/mylib-targets.cmake)
?
My worry is that the fetched repository version for the exported configuration may be different that the original one used for building my library (if the repository was updated between the original build of my library and its import in another project). Is there a solution / another way to handle this?
CPMAddPackage(
NAME fmt
GIT_TAG 10.2.1
GITHUB_REPOSITORY fmtlib/fmt
SYSTEM ON # used in case of cmake v3.25
OPTIONS "FMT_INSTALL YES" # create an installable target
)
Most projects does install itself only configured as subproject!
Thanks, but for some reason, even using CPMAddPackage, I still got the following error when I try to import my package in another project:
CMake Error at CMakeLists.txt:33 (find_package):
Found package configuration file:
......./install/lib/cmake/mylib/mylib-config.cmake
but it set mylib_FOUND to FALSE so package "mylib" is considered
to be NOT FOUND. Reason given by package:
The following imported targets are referenced, but are missing: fmt::fmt
Actually, an installable target is indeed created for fmt, in install/cmake/lib/cmake/fmt, along with install/cmake/lib/cmake/mylib.
But in a new project, I would expect that find_package(mylib) would handle this dependency. Perhaps I am doing something wrong in the install config of mylib?