Skip to content

Instantly share code, notes, and snippets.

@anderay
Created August 14, 2024 10:18
Show Gist options
  • Select an option

  • Save anderay/d1c724a67bd3e55190db54dbef252ed7 to your computer and use it in GitHub Desktop.

Select an option

Save anderay/d1c724a67bd3e55190db54dbef252ed7 to your computer and use it in GitHub Desktop.
CMake local package
When project contains several subprojects in subdirectories then problem with common packages might appear.
Common for gtest - structure
> Root
- CMakeLists.txt
> source
> test
- CMakeLists.txt
> gtest
- CMakeLists.txt
> dependencies
> dependency1 (requires gtest)
- CMakeLists.txt
Here 'test' and 'dependency1' both require 'gtest' via 'find_package(GTest REQUIRED)'.
In root CMakeLists.txt gtest is added using 'add_subdirectory(getest EXCLUDE_FROM_ALL)', similarly other dependencies.
During build gtest will not be found in 'dependency1' (since GTestConfig.cmake is created after find_package() is executed).
To overcome:
- add 'set(GTest_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake CACHE PATH "Path to GTest package config file")'
- cerate empty 'GTestConfig.cmake' file in 'cmake' directory under 'Root'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment