Search code examples
c++cmakecatch2

CMake script for Catch2 version 3.x


I have Catch2 v3.4.0 in a folder called d:/Libraries/Catch2-3.4.0.

I have my test project that should use Catch2 in a folder called d:/work/test_project.

I want to use the CMake integration, not the amalgamated files (catch_amalgamated.hpp and catch_amalgamated.cpp).

The CMakeList.txt of the test project contains the following (as described here):

find_package(Catch2 3 REQUIRED)
add_executable(test_project main.cpp)
target_link_libraries(test_project PRIVATE Catch2::Catch2WithMain)

What I get is:

CMake Error at test_project/CMakeLists.txt:2 (find_package):
  By not providing "FindCatch2.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Catch2", but
  CMake did not find one.

  Could not find a package configuration file provided by "Catch2" (requested
  version 3) with any of the following names:

    Catch2Config.cmake
    catch2-config.cmake

  Add the installation prefix of "Catch2" to CMAKE_PREFIX_PATH or set
  "Catch2_DIR" to a directory containing one of the above files.  If "Catch2"
  provides a separate development package or SDK, be sure it has been
  installed.

I tried:

  • settings Catch2_DIR to either d:/Libraries/Catch2-3.4.0 or d:/Libraries/Catch2-3.4.0/CMake, but same error.
  • setting Catch2_CHECK_PATHS to either of the two folders, with the same result.

How exactly do I include this new Catch2 v3?


Solution

  • The directory you use in Catch2_DIR or CMAKE_PREFIX_PATH must be the directory in which you installed your build of Catch2. You cannot use the directory where you cloned the Catch2 git repo unless that is also where you chose to install your build of it to. This is stated in the docs. If you want basic instructions for building and installing Catch2, then see the docs, but all you know about doing builds and installs with CMake applies- those docs are just a suggestion for people who have no idea what to do.