Add CMake example

This commit is contained in:
Alex Hirsch 2020-11-26 21:56:26 +01:00
parent eb0ea94898
commit 353e06c941
10 changed files with 17982 additions and 0 deletions

1
cmake-example/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build*

View File

@ -0,0 +1,42 @@
# Ideally you want to require the lowest CMake version possible for your
# project. But you have to test that manually, so just go with what you are
# currently using and adjust as you go.
cmake_minimum_required(VERSION 3.16)
# The name of the core library is used as project name.
project(mylib
VERSION 0.1.0
DESCRIPTION "An example CMake project"
LANGUAGES CXX)
# This allows us to include CMake modules located in `./cmake`.
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
# Note that we do not set *any* global properties here!
# A dedicated function is used for setting common compile flags and properties.
include(compile_flags)
# Catch2 is used for testing. We bring along the single header version.
add_subdirectory(vendor/Catch2)
# We start off by declaring our main library.
add_library(mylib src/foo.cpp)
mylib_cxx_flags(mylib)
target_include_directories(mylib PUBLIC include)
# Followed by the executable using the library. Targets get prefixed with the
# project name in order to avoid conflicts. We can still adjust the actual
# output name for the executable.
add_executable(mylib_myapp apps/myapp.cpp)
mylib_cxx_flags(mylib_myapp)
set_target_properties(mylib_myapp PROPERTIES OUTPUT_NAME myapp)
target_link_libraries(mylib_myapp PRIVATE mylib)
# We also declare a test runner, again targets are prefixed with the project
# name.
enable_testing()
add_executable(mylib_test_runner tests/test_runner.cpp tests/foo_test.cpp)
mylib_cxx_flags(mylib_test_runner)
target_link_libraries(mylib_test_runner PRIVATE mylib mylib_Catch2)
add_test(NAME mylib_test_runner COMMAND $<TARGET_FILE:mylib_test_runner>)

View File

@ -0,0 +1,10 @@
#include <cstdlib>
#include <iostream>
#include "mylib/foo.hpp"
int main()
{
std::cout << mylib::foo() << "\n";
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,15 @@
# Note that functions are also prefixed with the project name to prevent
# collisions.
function(mylib_cxx_flags target)
# Instead of setting a specific standard directly, we use the *compile
# features* to require (at least) C++17 standard.
target_compile_features(${target} PRIVATE cxx_std_17)
# Furthermore, we turn off compiler extensions.
set_target_properties(${target} PROPERTIES CXX_EXTENSIONS OFF)
# We enable some warnings, although only if we know the compiler actually
# supports these flags. The `$<...>` syntax is a *generator expression*.
target_compile_options(${target} PRIVATE
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
endfunction()

View File

@ -0,0 +1,10 @@
#ifndef MYLIB_FOO_HPP
#define MYLIB_FOO_HPP
namespace mylib {
int foo();
} // end namespace mylib
#endif // MYLIB_FOO_HPP

10
cmake-example/src/foo.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "mylib/foo.hpp"
namespace mylib {
int foo()
{
return 42;
}
} // end namespace mylib

View File

@ -0,0 +1,8 @@
#include "catch.hpp"
#include "mylib/foo.hpp"
TEST_CASE("Foo Test", "[foo]")
{
REQUIRE(mylib::foo() == 42);
}

View File

@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

View File

@ -0,0 +1,7 @@
# Since we bring along our own version of Catch2, we prefix the target with the
# project name. Alternatively we could check whether Catch2::Catch2 is already
# available and use that one instead of our own via `add_library(mylib_Catch2
# ALIAS Catch2::Catch2)`.
add_library(mylib_Catch2 INTERFACE)
target_include_directories(mylib_Catch2 INTERFACE .)

17877
cmake-example/vendor/Catch2/catch.hpp vendored Executable file

File diff suppressed because it is too large Load Diff