Skip to content

Commit 5c437cd

Browse files
committed
CMakeLists.txt: use FetchContent to add Catch2 as dependency
1 parent 32f45d5 commit 5c437cd

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

build/CMake/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ endforeach()
6464
# Defines a build target - the executable file and its dependencies (the source files collected above)
6565
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
6666

67+
# Include CMake's FetchContent module
68+
include(FetchContent)
69+
70+
# Add a dependency: unit testing framework Catch2
71+
# (https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md)
72+
73+
# FetchContent_Declare declares where and how to fetch the content (e.g., Git URL, tarball, local path), but does not yet download or unpack it.
74+
FetchContent_Declare(
75+
Catch2
76+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
77+
GIT_TAG v3.8.1)
78+
# FetchContent_MakeAvailable does the following:
79+
# 1. Download or copy the external content to a local directory (<binary_dir>/_deps/ by default).
80+
# 2. Extract/unpack it if it's an archive.
81+
# 3. Prepare it for inclusion in the build (e.g., by configuring it).
82+
# This step is what CMake refers to as "populating the content" — turning the declaration into actual usable files.
83+
FetchContent_MakeAvailable(Catch2)
84+
# Link to the Catch2 static library
85+
target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)
86+
6787
if (VISUAL_STUDIO)
6888
# set our project as the startup project in the Visual Studio solution
6989
set_property(

build/GNUMake/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
5050
CXXFLAGS+=-Wa,-mbig-obj
5151
endif
5252

53+
# Define CATCH2_SINGLE_HEADER macrodefinition to use the single-header
54+
# version of Catch2 unit testing framework (the macrodefinition is used in test.cpp file)
55+
CXXFLAGS+=-DCATCH2_SINGLE_HEADER
56+
5357
# Linking to libstdc++fs is required for use of the Filesystem library extensions in <experimental/filesystem>
5458
STDLIBS= \
5559
-lstdc++fs

src/test.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@
2727

2828
#include "nlohmann/json.hpp" // JSON parser
2929

30-
// Catch2 - a single header unit test framework
31-
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
32-
#include "Catch2/catch.hpp"
30+
// Catch2 - unit testing framework
31+
#ifdef CATCH2_SINGLE_HEADER
32+
// Use the single header version of Catch2
33+
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
34+
#include "Catch2/catch.hpp"
35+
#else
36+
// Use the static library version of Catch2
37+
#include <catch2/catch_test_macros.hpp>
38+
#endif
3339

3440
TEST_CASE("Karatsuba", "[karatsuba]")
3541
{

0 commit comments

Comments
 (0)