From 92f8b01dfa24866ce1669c4c1df892a05e13a574 Mon Sep 17 00:00:00 2001 From: Deniz Evrenci Date: Fri, 26 Jul 2019 16:34:14 +0900 Subject: [PATCH] Add the static library Catch2WithMain It should provide a shared impl for all targets that need to link against Catch2's implementation. However, due to limitations of C++ linking and Catch2's v2 implementation, this is only experimental and might not work under some circumstances. --- CMakeLists.txt | 8 +++++++- docs/slow-compiles.md | 19 +++++++++++++++++++ src/catch_with_main.cpp | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/catch_with_main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c18782d3..7beae099 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,12 @@ endif() # provide a namespaced alias for clients to 'link' against if catch is included as a sub-project add_library(Catch2::Catch2 ALIAS Catch2) +# Hacky support for compiling the impl into a static lib +add_library(Catch2WithMain src/catch_with_main.cpp) +target_link_libraries(Catch2WithMain PUBLIC Catch2) +add_library(Catch2::Catch2WithMain ALIAS Catch2WithMain) + + # Only perform the installation steps when Catch is not being used as # a subproject via `add_subdirectory`, or the destinations will break, # see https://github.com/catchorg/Catch2/issues/1373 @@ -121,7 +127,7 @@ if (NOT_SUBPROJECT) # create and install an export set for catch target as Catch2::Catch install( TARGETS - Catch2 + Catch2 Catch2WithMain EXPORT Catch2Targets DESTINATION diff --git a/docs/slow-compiles.md b/docs/slow-compiles.md index 230f5330..8dcbf6d2 100644 --- a/docs/slow-compiles.md +++ b/docs/slow-compiles.md @@ -5,6 +5,7 @@ [Short answer](#short-answer)
[Long answer](#long-answer)
[Practical example](#practical-example)
+[Using the static library Catch2WithMain](#using-the-static-library-catch2withmain)
[Other possible solutions](#other-possible-solutions)
Several people have reported that test code written with Catch takes much longer to compile than they would expect. Why is that? @@ -64,6 +65,24 @@ tests-factorial.cpp:11: failed: Factorial(0) == 1 for: 0 == 1 Failed 1 test case, failed 1 assertion. ``` + +## Using the static library Catch2WithMain + +Catch2 also provides a static library that implements the runner. Note +that this support is experimental, due to interactions between Catch2 v2 +implementation and C++ linking limitations. + +As with the `Catch2` target, the `Catch2WithMain` CMake target can be used +either from a subdirectory, or from installed build. + +### CMake +```cmake +add_executable(tests-factorial tests-factorial.cpp) + +target_link_libraries(tests-factorial Catch2::Catch2WithMain) +``` + + ## Other possible solutions You can also opt to sacrifice some features in order to speed-up Catch's compilation times. For details see the [documentation on Catch's compile-time configuration](configuration.md#other-toggles). diff --git a/src/catch_with_main.cpp b/src/catch_with_main.cpp new file mode 100644 index 00000000..4ed06df1 --- /dev/null +++ b/src/catch_with_main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include