catch2 - chunhualiao/public-docs GitHub Wiki
Catch2 is primarily a header-only library, especially in versions 2.x (which your project uses). This means you do not need to build or link against a separate Catch2 library binary; you simply include the appropriate header(s) in your test source files.
However, there is one important caveat: although Catch2 is header-only, you must provide a single translation unit (source file) in your test suite that defines either #define CATCH_CONFIG_MAIN
or #define CATCH_CONFIG_RUNNER
before including catch.hpp
. This causes Catch2 to generate the main()
function and all the necessary symbol definitions for the test runner. All other test files should just include catch.hpp
(without the macro) to use the test macros.
In summary:
- Yes, Catch2 is header-only (no separate library to link).
- You must provide a main implementation as described above for your test executables to link and run.
If you need guidance on how to structure your test files or CMake setup for Catch2, let me know!
homebrew's Catch2
If Catch2_DIR
is set to /opt/homebrew/lib/cmake/Catch2
, this means that Catch2 was installed via Homebrew and its CMake configuration files are located in that directory.
Where are the actual Catch2 headers?
- For Homebrew installations, the Catch2 headers are typically installed in
/opt/homebrew/include/catch2/
. - The CMake config directory (
/opt/homebrew/lib/cmake/Catch2
) is used by CMake to locate the package, but the headers you need to include in your code are in/opt/homebrew/include/catch2/
.
How to use in your project:
- You should add
/opt/homebrew/include
to your include path (e.g., withtarget_include_directories
in CMake). - In your source files, you can then use:
#include <catch2/catch.hpp>
Summary:
- CMake config:
/opt/homebrew/lib/cmake/Catch2
- Headers:
/opt/homebrew/include/catch2/
- Add
/opt/homebrew/include
to your include path to use Catch2 in your project.
If you want to automatically add this path in your CMake setup, let me know and I can provide the exact CMake code.
include in a cmake build process
Your compilation database shows that the Catch2 include path is not present in the compiler's -I
or -isystem
flags. This means that when your test source files include #include "catch2/catch.hpp"
, the compiler will not be able to find the header, resulting in either a build failure or the header being silently ignored (if not included at all).
How to fix this:
-
Ensure Catch2 is present in your source tree or as a dependency.
- If you are using a local copy of Catch2 (as the commented-out CMake code suggests), you should have a directory like
tests/catch2/
orbuild/tests/catch2/
containingcatch.hpp
. - If you use a system package or FetchContent, make sure you know the path to the Catch2 headers.
- If you are using a local copy of Catch2 (as the commented-out CMake code suggests), you should have a directory like
-
Add the Catch2 include path to your test targets.
- In your
tests/CMakeLists.txt
, you need to add the directory containingcatch.hpp
to thetarget_include_directories
for each test target. - For example, if you have
catch2/catch.hpp
inbuild/tests/catch2/
, add:
or, for each test executable:target_include_directories(test_common PRIVATE ${CMAKE_SOURCE_DIR}/build/tests/catch2)
target_include_directories(test_${testname} PRIVATE ${CMAKE_SOURCE_DIR}/build/tests/catch2)
- If you use a different path, adjust accordingly.
- In your
-
Verify the include in your test source files.
- Your test files should include Catch2 like this:
or, if the header is directly in the include path:#include "catch2/catch.hpp"
#include "catch.hpp"
- Your test files should include Catch2 like this:
-
Regenerate your build system and rebuild.
- After updating your CMake files, re-run CMake to regenerate the build system, then rebuild. The compilation database should now show the correct
-I
path for Catch2.
- After updating your CMake files, re-run CMake to regenerate the build system, then rebuild. The compilation database should now show the correct
Summary:
You need to add the directory containing Catch2 headers to the include path for your test targets in CMake. This will ensure the compiler can find and use Catch2 during the build.
If you want to proceed with this fix, toggle to Act mode and I can generate the exact CMake changes for you.