I’ve been learning cmake recently and managed to convert a couple personal projects of mine to leverage it. One project is a utility library with a test executable, and the other is the main project that leverages the utility library and makes its own executable. They’re in two separate git repos, and the main project includes the utility project as a submodule. The utility library carries other library dependencies with it, like fmt, through cmake’s target_link_libraries
. And the main project includes the utility project with cmake’s add_subdirectory
. This all works great, with cmake configuring itself and being able to build all executables from the main project.
Now though, I’d like to start adding 3rd party libraries via vcpkg. And I’ve been following some online tutorials for it. The simplest step was to pull in fmt via vcpkg for the utility project. And I’ve gotten this to work with the utility project on its own. It can install fmt from vcpkg and build the test executable.
However, the main project will no longer build. During cmake configuration, it complains that the find_package
call in the utility project can’t lookup the fmt library. I made sure to setup the toolchain cache variable before the main project’s project
call, and I thought that’d be enough to satisfy my dependency on the utility project.
So now I’m left with a couple options. Either spend time debugging what’s going on, or double down on vcpkg to add my utility project via a vcpkg overlay port. The latter sounds like it’d be good exercise, and maybe better in the long run, but it doesn’t feel like something that should be necessary. Hence my title, can I add a cmake dependency that itself leverages vcpkg to bring things in?
Here are the relevant files for the project setup.
utility_project root CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE FILEPATH "Attempt to set vcpkg path")
project(utility_project)
add_subdirectory(utility_project)
add_subdirectory(test)
utility_projectutility_project CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
file(GLOB_RECURSE src CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
find_package(fmt CONFIG REQUIRED)
add_library(utility_project STATIC "${src}")
target_link_libraries(utility_project fmt::fmt)
target_include_directories(utility_project PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
utility_projecttest CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
file(GLOB_RECURSE src CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_executable(utility_project_test "${src}")
target_link_libraries(utility_project_test utility_project)
target_include_directories(utility_project_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
utility_project vcpkg.json
{
"dependencies": [
"fmt"
]
}
main_project root CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE FILEPATH "Attempt to set vcpkg path")
project(main_project)
add_subdirectory("externals/utility_project")
add_subdirectory(src)
main_projectsrc CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
file(GLOB_RECURSE src CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_executable(main_project "${src}")
target_link_libraries(main_project utility_project)
target_include_directories(main_project PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
This creates the following error when I try to configure main_project
1> [CMake] CMake Error at C:/Users/me/projects/vcpkg/scripts/buildsystems/vcpkg.cmake:857 (_find_package):
1> [CMake] Could not find a package configuration file provided by "fmt" with any of
1> [CMake] the following names:
1> [CMake]
1> [CMake] fmtConfig.cmake
1> [CMake] fmt-config.cmake
1> [CMake]
1> [CMake] Add the installation prefix of "fmt" to CMAKE_PREFIX_PATH or set "fmt_DIR"
1> [CMake] to a directory containing one of the above files. If "fmt" provides a
1> [CMake] separate development package or SDK, be sure it has been installed.
I’d expect that cmake should install fmt automatically for me when it reads the vcpkg.json file that’s in the submodule. I encountered a similar error when I was first setting up the utility project, but clearing the cmake cache reset things and cleared it up. That doesn’t work when I trying to configure main_project.
ConstCuriosity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.