build-systems.md (2917B)
1 ## Build systems 2 3 The latest released version of doctest can be obtained from here: https://raw.githubusercontent.com/doctest/doctest/master/doctest/doctest.h 4 5 You can substitute ```master``` with ```dev``` or a tag like ```v1.4.8``` for a specific version in the URL above. 6 7 ### CMake 8 9 - **doctest** is easiest to use as a single file inside your own repository. Then the following minimal example will work: 10 11 ```cmake 12 cmake_minimum_required(VERSION 3.0) 13 project(cmake_test VERSION 0.0.1 LANGUAGES CXX) 14 15 # Prepare doctest for other targets to use 16 find_package(doctest REQUIRED) 17 18 # Make test executable 19 add_executable(tests main.cpp) 20 target_compile_features(test PRIVATE cxx_std_17) 21 target_link_libraries(test PRIVATE doctest::doctest) 22 ``` 23 24 - You can also use the following CMake snippet to automatically fetch the entire **doctest** repository from github and configure it as an external project: 25 26 ```cmake 27 include(ExternalProject) 28 find_package(Git REQUIRED) 29 30 ExternalProject_Add( 31 doctest 32 PREFIX ${CMAKE_BINARY_DIR}/doctest 33 GIT_REPOSITORY https://github.com/doctest/doctest.git 34 TIMEOUT 10 35 UPDATE_COMMAND ${GIT_EXECUTABLE} pull 36 CONFIGURE_COMMAND "" 37 BUILD_COMMAND "" 38 INSTALL_COMMAND "" 39 LOG_DOWNLOAD ON 40 ) 41 42 # Expose required variable (DOCTEST_INCLUDE_DIR) to parent scope 43 ExternalProject_Get_Property(doctest source_dir) 44 set(DOCTEST_INCLUDE_DIR ${source_dir}/doctest CACHE INTERNAL "Path to include folder for doctest") 45 ``` 46 47 And later you'll be able to use the doctest include directory like this: 48 49 ```cmake 50 # add it globally 51 include_directories(${DOCTEST_INCLUDE_DIR}) 52 53 # or per target 54 target_include_directories(my_target PUBLIC ${DOCTEST_INCLUDE_DIR}) 55 ``` 56 57 - If you have the entire doctest repository available (as a submodule or just as files) you could also include it in your CMake build by using ```add_subdirectory(path/to/doctest)``` and then you could use it like this: 58 59 ```cmake 60 add_executable(my_tests src_1.cpp src_2.cpp ...) 61 target_link_libraries(my_tests doctest) 62 ``` 63 64 - The ```CMakeLists.txt``` file of the doctest repository has ```install()``` commands so you could also use doctest as a package. 65 66 - To discover tests from an executable and register them in ctest you could use [```doctest_discover_tests(<target>)``` from scripts/cmake/doctest.cmake](../../scripts/cmake/doctest.cmake) - read the comments in the file on how to use it. It works just like [the same functionality in Catch](https://github.com/catchorg/Catch2/blob/master/docs/cmake-integration.md#automatic-test-registration). 67 68 ### Package managers 69 70 **doctest** is available through the following package managers: 71 72 - vcpkg 73 - hunter 74 - conan 75 - https://conan.io/center/doctest 76 - https://github.com/conan-io/conan-center-index/tree/master/recipes/doctest 77 - Homebrew (`brew install doctest`) 78 79 --- 80 81 [Home](readme.md#reference) 82 83 <p align="center"><img src="../../scripts/data/logo/icon_2.svg"></p>