CMakeLists.txt
description and generates native build files for Make, Ninja, MSBuild, or Xcode.
All C and C++ projects in Project Story use CMake so the same source tree builds on
Windows, Linux, and macOS without modification.
-S . - source directory (where CMakeLists.txt lives).-B build - out-of-source build directory; keeps the source tree clean.--build build - invokes the underlying generator (Make or Ninja or MSBuild).-DCMAKE_BUILD_TYPE=Release - enables optimizations; use Debug for debug symbols.-D)
CMAKE_BUILD_TYPE=Release|Debug|RelWithDebInfo|MinSizeRelRelWithDebInfo
gives optimized code with debug symbols attached.
CMAKE_CXX_STANDARD=17|20|23 and
CMAKE_CXX_STANDARD_REQUIRED=ONREQUIRED=ON makes
CMake error if the compiler cannot satisfy the version.
CMAKE_EXPORT_COMPILE_COMMANDS=ONcompile_commands.json to the build directory.
Copy or symlink it to the project root so clangd picks it up automatically.
CMAKE_CXX_COMPILER=/path/to/clang++CMAKE_INSTALL_PREFIX=/usr/localcmake --install. On Windows the default is
C:/Program Files/<project>.
BUILD_SHARED_LIBS=ONadd_library produce a shared library (.dll / .so)
instead of static unless the call explicitly says STATIC.
-G)
CMakeLists.txt commands
cmake_minimum_required(VERSION 3.20)project(MyApp VERSION 1.0 LANGUAGES CXX)add_executable(target src1.cpp src2.cpp)add_library(lib STATIC|SHARED|INTERFACE src.cpp)STATIC produces a .lib/.a; SHARED produces
a .dll/.so; INTERFACE carries usage requirements only
(no compiled output - useful for header-only libraries).
target_link_libraries(target PRIVATE|PUBLIC|INTERFACE dep)dep to target. PRIVATE
keeps the dependency internal; PUBLIC propagates it
to anything that links against target.
target_include_directories(target PUBLIC include/)PUBLIC propagates the path to
dependents; PRIVATE is target-only.
target_compile_options(target PRIVATE -Wall -Wextra -Wpedantic)target_compile_features(target PUBLIC cxx_std_20)CMAKE_CXX_STANDARD; preferred
in library CMakeLists.txt to avoid overriding the caller's setting.
option(ENABLE_TESTS "Build unit tests" ON)-DENABLE_TESTS=OFF
at configure time.
add_subdirectory(part1)CMakeLists.txt.
Targets defined there become visible to the parent.
find_package(nlohmann_json 3.11 REQUIRED)nlohmann_json::nlohmann_json)
for use in target_link_libraries.
enable_testing() / add_test(NAME t COMMAND ./tests)ctest --test-dir build.
message(STATUS|WARNING|FATAL_ERROR "text")FATAL_ERROR stops
configuration immediately.
.csproj,
.vcxproj, and .sln files. You rarely write MSBuild XML directly -
the .NET SDK and VS generate it - but knowing the key flags matters when scripting builds
or debugging build failures.
/p:Configuration=Release - selects the Release configuration (optimized)./p:Platform=x64 - targets 64-bit; omit for SDK-style projects that default to AnyCPU./t:Rebuild - clean then build; /t:Build is incremental./m - parallel build using all CPU cores.dotnet CLI (section 1.5) wraps MSBuild and is
the preferred command-line interface. Use MSBuild directly for legacy .vcxproj
C++ projects or when you need fine-grained control over the target graph.
/p:Property=Value/t:Target1;Target2Build
if omitted.
/m[:N]/m alone
uses all logical CPU cores.
/v:quiet|minimal|normal|detailed|diagnosticminimal shows only warnings
and errors; diagnostic dumps every property and task.
/blmsbuild.binlog). Open with the
MSBuild Structured Log Viewer
to diagnose property evaluation and target execution order.
/nologo/p: properties
/p:Configuration=Debug|Release/p:Platform=x86|x64|AnyCPU|ARM64AnyCPU; C++ projects require an explicit match.
/p:OutDir=path\/p:TreatWarningsAsErrors=true/p:Optimize=true|false/p:DefineConstants=TRACE;MYFEATURE/t: targets
/t:Build - incremental build; skips up-to-date inputs (default)./t:Rebuild - deletes all outputs then builds from scratch./t:Clean - removes all build outputs without rebuilding./t:Restore - restores NuGet packages; runs before the first build on a fresh clone./t:Publish - produces deployment-ready output; respects PublishProfile..csproj elements
<OutputType>Exe|Library|WinExe</OutputType>Exe builds a console app; Library a .dll;
WinExe a Windows GUI app with no console window.
<TargetFramework>net10.0</TargetFramework><TargetFrameworks>
(plural) to multi-target: net8.0;net10.0.
<Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings>using directives (System, LINQ, etc.)
so they don't appear in every source file.
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>unsafe { } block.
<PackageReference Include="Pkg" Version="1.2.3" />dotnet restore or
msbuild /t:Restore to download it.
<ProjectReference Include="../Lib/Lib.csproj" />-Wall -Wextra -std=c++20 -O2 as a baseline.
CMAKE_CXX_CLANG_TIDY.
.clang-format in the project root. Integrates with VS Code's
"Format on Save."
-fsanitize=address,undefined
to catch memory errors and undefined behavior at runtime.
compile_commands.json from CMake with
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON. Copy or symlink it to the project root
so clangd finds it automatically.
-std=c++17|c++20|c++23-std=c++20 as the
baseline for new projects.
-Wall -Wextra -Wpedantic-Wall enables the most common warnings;
-Wextra adds a second tier; -Wpedantic
enforces strict ISO conformance. Use all three together.
-Werror-O0|-O1|-O2|-O3|-Os|-Og-O2 is the standard release
setting. -Og enables optimizations that do not
impair debugger output.
-g / -g3-g3 includes macro definitions.
Combine with -O0 or -Og for debugging.
-fsanitize=address,undefined-fsanitize=thread.
-fsanitize=thread-I include/-DNDEBUG / -DMY_MACRO=1-DNDEBUG disables
assert() checks.
-c--checks=bugprone-*,modernize-*,readability-*-
to disable it: -modernize-use-trailing-return-type.
--fixgit diff before committing.
--warnings-as-errors=*set(CMAKE_CXX_CLANG_TIDY "clang-tidy;--checks=bugprone-*")--style=file.clang-format
file walking up the directory tree. Use this in projects.
--style=LLVM|Google|Chromium|Microsoft|Mozilla--dump-config to
generate a starting .clang-format to customize.
-i-i, formatted output
goes to stdout for preview.
--dry-run --Werrorcompile_commands.json at the project root
(or pointed to via --compile-commands-dir). Generate
it from CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON.
.clangd file at the root.
Common uses: suppress specific diagnostics, add extra compile flags,
or exclude generated files from indexing.
CompileFlags: { Add: [-std=c++20, -DMY_FLAG] } in
.clangd - appends flags to every translation unit
without modifying CMakeLists.txt.
Diagnostics: { Suppress: [unused-includes] } in
.clangd - silences noisy checks project-wide.
dotnet CLI is the primary interface for .NET SDK projects. It creates,
builds, tests, and runs C# projects without requiring Visual Studio.
dotnet new - scaffolds projects; dotnet new list shows all templates.dotnet add package <name> - adds a NuGet dependency and updates the .csproj.dotnet publish --self-contained - bundles the runtime into the output; no .NET install needed on the target.dotnet watch run - hot-reload loop; rebuilds and restarts on file save..csproj files are concise enough to edit by hand. The CLI
and MSBuild share the same project model, so a project built with
dotnet build opens without modification in Visual Studio.
-r <rid>win-x64, linux-x64,
osx-arm64, etc. Required for self-contained publish.
--self-contained/p:PublishSingleFile=true/p:PublishTrimmed=true-c|--configuration Debug|Release - build configuration; defaults to Debug.-o|--output <path> - override the output directory.--no-build - skip the build step (test, run, publish).--no-restore - skip NuGet restore; useful when packages are already cached.-v|--verbosity quiet|minimal|normal|detailed|diagnostic - controls log level.dotnet --info - shows installed SDK versions, runtime paths, and OS details.dotnet --list-sdks / dotnet --list-runtimes - enumerate installed versions.pip freeze > requirements.txt
records the exact versions for reproducible installs.
setup.py. Build tools (flit, hatchling, setuptools) read it.
python -m modulepython script.py for tools like
venv, pip, and pytest
because it guarantees the correct environment is used.
python -c "code"python -c "import sys; print(sys.path)".
mypy --strict--disallow-untyped-defs
and --warn-return-any. Start without --strict
on existing code and tighten incrementally.
ruffpyproject.toml under [tool.ruff].
Significantly faster than pylint on large codebases.