sudo apt install g++) (ppa:ubuntu-toolchain-r/test)
sudo apt install clang) (apt.llvm.org)brew install llvm)brew install gcc)
CMakeLists.txt)
meson.build)
BUILD)
SConstruct)
xmake.lua)
autogen.sh,
configure.sh)
pkg-config)Makefile)
build.ninja)
poly
├── src -- library source code
│ ├── include -- public API header files
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src -- implementation files
│ │ └── interpolate.cpp
│ └── CMakeLists.txt -- library build script
├── test -- unit tests
│ ├── CMakeLists.txt -- testing framework build script
│ └── test-interpolate.cpp
└── CMakeLists.txt -- user-facing build script
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt
# Configure
cmake -S . -B build -G "Ninja Multi-Config" -D POLY_INIT_NAN=On
# Build
cmake --build build --config Debug -j
# Run tests
cmake --build build --config Debug --target test
# Configure
cmake -S . -B build -G "Ninja Multi-Config" -D POLY_INIT_NAN=On
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake (found version "1.11.0")
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /home/pieter/GitHub/Cpp-Presentation/code/poly/build
# Build
cmake --build build --config Debug -j
[1/4] Building CXX object test/CMakeFiles/tests.dir/Debug/test-interpolate.cpp.o
[2/4] Building CXX object src/CMakeFiles/poly.dir/Debug/src/interpolate.cpp.o
[3/4] Linking CXX static library src/Debug/libpoly.a
[4/4] Linking CXX executable test/Debug/tests
# Run tests
cmake --build build --config Debug --target test
[0/1] Running tests...
Test project /home/pieter/GitHub/Cpp-Presentation/code/poly/build
Start 1: poly.interpolateCheby
1/1 Test #1: poly.interpolateCheby ............ Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec
find_package(name)add_library(target sources...)add_executable(target sources...)add_custom_target(target command args...)target_compile_features(target SCOPE features...)
target_compile_definitions(target SCOPE definitions...)
target_compile_options(target SCOPE options...)
target_include_directories(target SCOPE directories...)
target_link_options(target SCOPE options...)
set_target_properties(targets PROPERTIES property value)target_link_libraries(target SCOPE dependency)
PUBLIC
PRIVATE
INTERFACE
PUBLIC |
PRIVATE |
INTERFACE |
|
|---|---|---|---|
liba |
-flag |
-flag |
|
libb |
-flag |
-flag |
poly example from earlier:
PRIVATE
# Interface libraries have no source files, only compiler flags etc.
add_library(warnings INTERFACE)
# Add compiler warnings for GCC
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(warnings INTERFACE "-Wall" "-Wextra" "-pedantic")
endif()
# Create an actual library target
add_library(poly "src/interpolate.cpp" ...)
# Apply warnings only when building poly, not when building dependents
target_link_libraries(poly PRIVATE warnings)
# This could be a user program that uses the poly library
add_executable(user_program "main.cpp")
# Compiled without warnings, thanks to private link between poly and warnings
target_link_libraries(user_program PRIVATE poly)
add_subdirectory)
conanfile.txt or conanfile.py fileCMakeDeps generator to make packages available using
find_package
CMakeToolchain generator to pass options to CMake using a toolchain
file
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
├── conanfile.txt
└── CMakeLists.txt
[requires]
eigen/3.4.0
[test_requires]
gtest/1.12.1
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
conan profile detect --force
poly project
conan install . # Default generator and default build type
conan install . \
-c tools.cmake.cmaketoolchain:generator="Ninja" \
-s build_type=Debug \
--build=missing
cmake --preset conan-debug # Configure
cmake --build --preset conan-debug # Build
ctest --preset conan-debug # Run tests
cmake --preset conan-debug # Configure
Preset CMake variables:
CMAKE_BUILD_TYPE="Debug"
CMAKE_POLICY_DEFAULT_CMP0091="NEW"
CMAKE_TOOLCHAIN_FILE:FILEPATH="~/poly/build/Debug/generators/conan_toolchain.cmake"
-- Using Conan toolchain: ~/poly/build/Debug/generators/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 17 with extensions ON
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Component target declared 'Eigen3::Eigen'
-- Conan: Component target declared 'GTest::gtest'
-- Conan: Component target declared 'GTest::gtest_main'
-- Conan: Component target declared 'GTest::gmock'
-- Conan: Component target declared 'GTest::gmock_main'
-- Conan: Target declared 'gtest::gtest'
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: ~/poly/build/Debug
cmake --build --preset conan-debug # Build
[1/4] Building CXX object test/CMakeFiles/tests.dir/Debug/test-interpolate.cpp.o
[2/4] Building CXX object src/CMakeFiles/poly.dir/Debug/src/interpolate.cpp.o
[3/4] Linking CXX static library src/Debug/libpoly.a
[4/4] Linking CXX executable test/Debug/tests
ctest --preset conan-debug # Run tests
[0/1] Running tests...
Test project ~/poly/build
Start 1: poly.interpolateCheby
1/1 Test #1: poly.interpolateCheby ............ Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec
conanfile.txt, flexible and extensible using Python
scripting in conanfile.pyvcpkg.json manifest file, CMake scripting in
portfile.cmake
-Wall -Wextra -Wpedantic -pedantic-errors absolute minimum/W3 absolute minimum, preferably /W4-Werror/WX
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wxxx"
/* code that would otherwise raise warning xxx */
#pragma GCC diagnostic pop
#pragma warning(push)
#pragma warning(disable:xxxx)
/* code that would otherwise raise warning xxxx */
#pragma warning(pop)
-Wno-error=xxx suppresses errors for warning -Wxxx,
-Wno-xxx disables warning entirely-fdiagnostics-show-option to include the warning name in the
message
/wdxxxx disables warning with code xxxx
-D CMAKE_CXX_CLANG_TIDY='clang-tidy;--warnings-as-errors=*' (cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html)
NOLINT(name-of-check) comments
HeaderFilterRegex so it also checks your header files!
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
├── .clang-tidy
├── conanfile.txt
└── CMakeLists.txt
---
WarningsAsErrors: ''
HeaderFilterRegex: '(include/poly)'
FormatStyle: file
Checks: |
*,
-abseil*,-altera*,-android*,-fuchsia*,-google*,-llvm*,-zircon*,
-modernize-use-trailing-return-type,
-*-magic-numbers,
google-build-using-namespace,
CheckOptions:
- key: readability-implicit-bool-conversion.AllowPointerConditions
value: true
-fanalyzer option
-fsanitize=address)
-fsanitize=undefined)
-fsanitize=thread)
GDB TUI
layout).gitignore
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
├── .clang-format
├── .clang-tidy
├── conanfile.txt
└── CMakeLists.txt
perf
profiles, github.com/Netflix/flamescope)
Release mode (with optimizations enabled)-march=native, to get SSE/AVX supportCMAKE_INTERPROCEDURAL_OPTIMIZATION=On)Debug mode is slow, use -Og instead of -O0pybind11 (github.com/pybind/pybind11)
nanobind (github.com/wjakob/nanobind)
pandas or scikit-learnmatplotlibpy-build-cmake (github.com/tttapa/py-build-cmake)