diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f7aba30d34184211514d40433e82f62432899366 --- /dev/null +++ b/code/CMakeLists.txt @@ -0,0 +1,45 @@ +# +# Main project for building all of the exercises at once. +# + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( cpluspluscourse LANGUAGES CXX ) + +# Make sure that the project is built "out of source". As an "in source" build +# would interfere with the simple Makefiles coming with the code. +if( "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}" ) + message( FATAL_ERROR "The tutorial code must be built out of source!" ) +endif() + +# Add a custom target for building all the solutions. Which are not built by +# default. +add_custom_target( solution ) + +# Include the test (hello world) project. +add_subdirectory( hello ) + +# Include the exercises that (should) work on all platforms. +add_subdirectory( callgrind ) +add_subdirectory( constness ) +add_subdirectory( cppcheck ) +add_subdirectory( debug ) +add_subdirectory( memcheck ) +add_subdirectory( move ) +add_subdirectory( polymorphism ) +add_subdirectory( templates ) +add_subdirectory( valgrind ) +add_subdirectory( virtual_inheritance ) + +# Include the non-Windows-native exercises. +if( NOT MSVC ) + add_subdirectory( helgrind ) + add_subdirectory( python ) + add_subdirectory( race ) +endif() + +# Include the gcc-only exercises. +if( NOT APPLE AND NOT MSVC ) + add_subdirectory( lambdas ) + add_subdirectory( stl ) +endif() diff --git a/code/CompilerSettings.cmake b/code/CompilerSettings.cmake new file mode 100644 index 0000000000000000000000000000000000000000..01d2208a0057a557df06e732b4952491c76697a2 --- /dev/null +++ b/code/CompilerSettings.cmake @@ -0,0 +1,34 @@ +# +# Small module used in every project to set up the default "compilation +# environment". +# + +# Guard this file against multiple inclusions. +get_property( _compilersSet GLOBAL PROPERTY COMPILER_SETTINGS_DONE SET ) +if( _compilersSet ) + unset( _compilersSet ) + return() +endif() +set_property( GLOBAL PROPERTY COMPILER_SETTINGS_DONE TRUE ) + + +# Set up a Debug build type by default, if the user didn't ask for something +# else. +if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES ) + set( CMAKE_BUILD_TYPE "Debug" CACHE + STRING "Choose the type of build." FORCE ) + set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Release" "MinSizeRel" "RelWithDebInfo" ) +endif() + +# Use C++17 in the project by default, or as high of a value as possible. +set( CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to use" ) +set( CMAKE_CXX_EXTENSIONS FALSE CACHE BOOL "(Dis)Allow C++ extensions" ) + +# Enable (almost) all warnings for the build. +if( ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" ) OR + ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) ) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra" ) +elseif( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4" ) +endif() diff --git a/code/callgrind/CMakeLists.txt b/code/callgrind/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a24b182976fdf99d1b7cccd0d07762d026b9de8d --- /dev/null +++ b/code/callgrind/CMakeLists.txt @@ -0,0 +1,16 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( callgrind LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( fibocrunch fibocrunch.cpp ) + +# Create the "solution executable". +add_executable( fibocrunch.sol EXCLUDE_FROM_ALL solution/fibocrunch.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution fibocrunch.sol ) +endif() diff --git a/code/constness/CMakeLists.txt b/code/constness/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9168fa40ca2647b45d54fe7f9f09da0ca3325613 --- /dev/null +++ b/code/constness/CMakeLists.txt @@ -0,0 +1,16 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( constness LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( constplay constplay.cpp ) + +# Create the "solution executable". +add_executable( constplay.sol EXCLUDE_FROM_ALL solution/constplay.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution constplay.sol ) +endif() diff --git a/code/cppcheck/CMakeLists.txt b/code/cppcheck/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..246afff2a5c38ee41a2b63b55eff7dc5389dac88 --- /dev/null +++ b/code/cppcheck/CMakeLists.txt @@ -0,0 +1,17 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( cppcheck LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( cppcheck_randomize randomize.cpp ) + +# Create the "solution executable". +add_executable( cppcheck_randomize.sol EXCLUDE_FROM_ALL + solution/randomize.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution cppcheck_randomize.sol ) +endif() diff --git a/code/debug/CMakeLists.txt b/code/debug/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..8b0a9f2501cfb9d2ca5a0d56b7c133edc4decf56 --- /dev/null +++ b/code/debug/CMakeLists.txt @@ -0,0 +1,17 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( debug LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( debug_randomize randomize.cpp ) + +# Create the "solution executable". +add_executable( debug_randomize.sol EXCLUDE_FROM_ALL + solution/randomize.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution debug_randomize.sol ) +endif() diff --git a/code/helgrind/CMakeLists.txt b/code/helgrind/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5c8c2cbc0ab13a8e49aa656985c7aa12e5b74ca0 --- /dev/null +++ b/code/helgrind/CMakeLists.txt @@ -0,0 +1,21 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( helgrind LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Figure out how to use the platform's thread capabilities. +find_package( Threads REQUIRED ) + +# Create the user's executable. +add_executable( fiboMT fiboMT.cpp ) +target_link_libraries( fiboMT PRIVATE Threads::Threads ) + +# Create the "solution executable". +add_executable( fiboMT.sol EXCLUDE_FROM_ALL solution/fiboMT.sol.cpp ) +target_link_libraries( fiboMT.sol PRIVATE Threads::Threads ) +if( TARGET solution ) + add_dependencies( solution fiboMT.sol ) +endif() diff --git a/code/hello/CMakeLists.txt b/code/hello/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6b8dea666b8db8286210c4e677bac389e68b4f49 --- /dev/null +++ b/code/hello/CMakeLists.txt @@ -0,0 +1,15 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( hello LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Set up the library. +add_library( helloLib hello.hpp hello.cpp ) +set_target_properties( helloLib PROPERTIES OUTPUT_NAME "hello" ) + +# Set up the executable. +add_executable( hello main.cpp ) +target_link_libraries( hello PRIVATE helloLib ) diff --git a/code/lambdas/CMakeLists.txt b/code/lambdas/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c52cf281c396b99d391017316dbbd6695b611613 --- /dev/null +++ b/code/lambdas/CMakeLists.txt @@ -0,0 +1,19 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( lambdas LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( lambdas_randomize Complex.hpp randomize.cpp ) + +# Create the "solution executable". +add_executable( lambdas_randomize.sol EXCLUDE_FROM_ALL + Complex.hpp solution/randomize.sol.cpp ) +target_include_directories( lambdas_randomize.sol PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} ) +if( TARGET solution ) + add_dependencies( solution lambdas_randomize.sol ) +endif() diff --git a/code/memcheck/CMakeLists.txt b/code/memcheck/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5d3dba772ad7df9a3333f91403095131cbdc6d7e --- /dev/null +++ b/code/memcheck/CMakeLists.txt @@ -0,0 +1,27 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( memcheck LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's library. +add_library( memcheckPoly Polygons.hpp Polygons.cpp ) +set_target_properties( memcheckPoly PROPERTIES OUTPUT_NAME "poly" ) + +# Create the user's executable. +add_executable( memleak memleak.cpp ) +target_link_libraries( memleak PRIVATE memcheckPoly ) + +# Create the "solution library". +add_library( memcheckPolySol EXCLUDE_FROM_ALL + solution/Polygons.sol.hpp solution/Polygons.sol.cpp ) +set_target_properties( memcheckPolySol PROPERTIES OUTPUT_NAME "polysol" ) + +# Create the "solution executable". +add_executable( memleak.sol EXCLUDE_FROM_ALL solution/memleak.sol.cpp ) +target_link_libraries( memleak.sol PRIVATE memcheckPolySol ) +if( TARGET solution ) + add_dependencies( solution memleak.sol ) +endif() diff --git a/code/move/CMakeLists.txt b/code/move/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c8f3b1267bcef35b57c9bd55bc1b45465c586cc8 --- /dev/null +++ b/code/move/CMakeLists.txt @@ -0,0 +1,17 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( move LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( trymove NVector.hpp trymove.cpp ) + +# Create the "solution executable". +add_executable( trymove.sol EXCLUDE_FROM_ALL + solution/NVector.sol.hpp solution/trymove.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution trymove.sol ) +endif() diff --git a/code/polymorphism/CMakeLists.txt b/code/polymorphism/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..8fde9b6b9cf2dc0bfd30b029075e68796a65a3d4 --- /dev/null +++ b/code/polymorphism/CMakeLists.txt @@ -0,0 +1,24 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( polymorhism LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's library. +add_library( polymorphismPoly Polygons.hpp Polygons.cpp ) +target_include_directories( polymorphismPoly PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} ) +set_target_properties( polymorphismPoly PROPERTIES OUTPUT_NAME "poly" ) + +# Create the user's executable. +add_executable( trypoly trypoly.cpp ) +target_link_libraries( trypoly PRIVATE polymorphismPoly ) + +# Create the "solution executable". +add_executable( trypoly.sol EXCLUDE_FROM_ALL solution/trypoly.sol.cpp ) +target_link_libraries( trypoly.sol PRIVATE polymorphismPoly ) +if( TARGET solution ) + add_dependencies( solution trypoly.sol ) +endif() diff --git a/code/python/CMakeLists.txt b/code/python/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0c59655664ba82388d847d4e2c8fede616aa941b --- /dev/null +++ b/code/python/CMakeLists.txt @@ -0,0 +1,24 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.12 ) +project( python LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Find Python for the build. +find_package( Python3 COMPONENTS Development REQUIRED ) + +# Build the C++ shared library. +add_library( mandel SHARED Complex.hpp mandel.hpp mandel.cpp ) + +# Build a "C wrapper" around the C++ shared library. +add_library( mandelc SHARED mandel_cwrapper.hpp mandel_cwrapper.cpp ) +target_link_libraries( mandelc PUBLIC mandel ) + +# Build the Python module around the C++ shared library. +add_library( mandelm SHARED mandel_module.cpp ) +target_link_libraries( mandelm PRIVATE Python3::Python mandel ) +set_target_properties( mandelm PROPERTIES + PREFIX "" + OUTPUT_NAME "mandel" ) diff --git a/code/race/CMakeLists.txt b/code/race/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1ee4ff57561f26f5d412457e1ba23bcf9eb61c10 --- /dev/null +++ b/code/race/CMakeLists.txt @@ -0,0 +1,21 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( race LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Figure out how to use the platform's thread capabilities. +find_package( Threads REQUIRED ) + +# Create the user's executable. +add_executable( racing racing.cpp ) +target_link_libraries( racing PRIVATE Threads::Threads ) + +# Create the "solution executable". +add_executable( racing.sol EXCLUDE_FROM_ALL solution/racing.sol.cpp ) +target_link_libraries( racing.sol PRIVATE Threads::Threads ) +if( TARGET solution ) + add_dependencies( solution racing.sol ) +endif() diff --git a/code/stl/CMakeLists.txt b/code/stl/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b74d91f02dab5202270c677a1684b81aa54c3b44 --- /dev/null +++ b/code/stl/CMakeLists.txt @@ -0,0 +1,20 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( stl LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable(s). +add_executable( stl_randomize Complex.hpp randomize.cpp ) +add_executable( stl_randomize.nostl Complex.hpp randomize.nostl.cpp ) + +# Create the "solution executable". +add_executable( stl_randomize.sol EXCLUDE_FROM_ALL + Complex.hpp solution/randomize.sol.cpp ) +target_include_directories( stl_randomize.sol PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} ) +if( TARGET solution ) + add_dependencies( solution stl_randomize.sol ) +endif() diff --git a/code/templates/CMakeLists.txt b/code/templates/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ceaf70c5c4502ed88beacd59a2191e3c79659aa7 --- /dev/null +++ b/code/templates/CMakeLists.txt @@ -0,0 +1,19 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( templates LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( playwithsort Complex.hpp OrderedVector.hpp playwithsort.cpp ) + +# Create the "solution executable". +add_executable( playwithsort.sol EXCLUDE_FROM_ALL + Complex.hpp solution/OrderedVector.sol.hpp solution/playwithsort.sol.cpp ) +target_include_directories( playwithsort.sol PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} ) +if( TARGET solution ) + add_dependencies( solution playwithsort.sol ) +endif() diff --git a/code/valgrind/CMakeLists.txt b/code/valgrind/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0ed6e796f1461dcc950522e889af4f3c5c6a6900 --- /dev/null +++ b/code/valgrind/CMakeLists.txt @@ -0,0 +1,17 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( valgrind LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's executable. +add_executable( valgrind_randomize randomize.cpp ) + +# Create the "solution executable". +add_executable( valgrind_randomize.sol EXCLUDE_FROM_ALL + solution/randomize.sol.cpp ) +if( TARGET solution ) + add_dependencies( solution valgrind_randomize.sol ) +endif() diff --git a/code/virtual_inheritance/CMakeLists.txt b/code/virtual_inheritance/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..eae9f5a206ba1db505204e2d98d5ac05ea503bdf --- /dev/null +++ b/code/virtual_inheritance/CMakeLists.txt @@ -0,0 +1,23 @@ + +# Set up the project. +cmake_minimum_required( VERSION 3.1 ) +project( virtual_inheritance LANGUAGES CXX ) + +# Set up the compilation environment. +include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) + +# Create the user's library. +add_library( textbox TextBox.hpp TextBox.cpp ) +target_include_directories( textbox PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) + +# Create the user's executable. +add_executable( trymultiherit trymultiherit.cpp ) +target_link_libraries( trymultiherit PRIVATE textbox ) + +# Create the "solution executable". +add_executable( trymultiherit.sol EXCLUDE_FROM_ALL + solution/trymultiherit.sol.cpp ) +target_link_libraries( trymultiherit.sol PRIVATE textbox ) +if( TARGET solution ) + add_dependencies( solution trymultiherit.sol ) +endif()