Add CMake example
This commit is contained in:
		
							
								
								
									
										1
									
								
								cmake-example/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								cmake-example/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					/build*
 | 
				
			||||||
							
								
								
									
										42
									
								
								cmake-example/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								cmake-example/CMakeLists.txt
									
									
									
									
									
										Normal 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>)
 | 
				
			||||||
							
								
								
									
										10
									
								
								cmake-example/apps/myapp.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								cmake-example/apps/myapp.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					#include <cstdlib>
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mylib/foo.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::cout << mylib::foo() << "\n";
 | 
				
			||||||
 | 
						return EXIT_SUCCESS;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										15
									
								
								cmake-example/cmake/compile_flags.cmake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								cmake-example/cmake/compile_flags.cmake
									
									
									
									
									
										Normal 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()
 | 
				
			||||||
							
								
								
									
										10
									
								
								cmake-example/include/mylib/foo.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								cmake-example/include/mylib/foo.hpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										10
									
								
								cmake-example/src/foo.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					#include "mylib/foo.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace mylib {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int foo()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return 42;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // end namespace mylib
 | 
				
			||||||
							
								
								
									
										8
									
								
								cmake-example/tests/foo_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								cmake-example/tests/foo_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					#include "catch.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mylib/foo.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("Foo Test", "[foo]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						REQUIRE(mylib::foo() == 42);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										2
									
								
								cmake-example/tests/test_runner.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								cmake-example/tests/test_runner.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					#define CATCH_CONFIG_MAIN
 | 
				
			||||||
 | 
					#include "catch.hpp"
 | 
				
			||||||
							
								
								
									
										7
									
								
								cmake-example/vendor/Catch2/CMakeLists.txt
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								cmake-example/vendor/Catch2/CMakeLists.txt
									
									
									
									
										vendored
									
									
										Normal 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
									
								
							
							
						
						
									
										17877
									
								
								cmake-example/vendor/Catch2/catch.hpp
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user