§The Annotated C++ StandardC++ 标准注疏 中文

Toolchain & Engineering

Compilers, linkers, ABI, build systems and project engineering.

Toolchain

From source to executableThe pipeline behind a single clang++ command: which subcommands the driver spawns, which phase of translation preprocessing, the AST, LLVM IR, assembly, object files and linking each correspond to, and how the header and module compilation models differ.drafttoolchaincompilation pipelineClangpreprocessingObject files, symbols and linkingWhat object files contain, how C++ linkage and the ODR turn into symbol binding, COMDAT groups and weak definitions, what undefined references and duplicate definitions are in the standard, which violations the linker silently accepts, and how static libraries, shared libraries and symbol visibility behave.drafttoolchainlinkingsymbolsELFName mangling (Itanium C++ ABI)How GCC and Clang encode C++ functions, variables and template specializations into symbol names the linker can compare, and how that encoding corresponds to the standard's notion of a signature.drafttoolchainABIItaniumname manglingModules in practiceHow compilers implement C++20 modules: each compiler's BMI, Clang's two compilation modes and reduced BMIs, dependency scanning, CMake's CXX_MODULES file sets, the state of import std and header units, and how module attachment shows up in symbol names.draftmodulesbuildObject layout and vtables (Itanium C++ ABI)The standard specifies only a few rules about the memory layout of class objects and leaves the rest to the ABI; this page uses Clang's layout and vtable dumps to show how data members, empty bases, vptrs, vtables, thunks and virtual bases are laid out and work under the Itanium ABI.drafttoolchainABIItaniumobject layoutHow C++ exceptions are implementedThe table-driven exception handling of the Itanium ABI that GCC and Clang use on non-Windows platforms: which runtime functions a throw calls, how two-phase unwinding finds a handler, how landing pads destroy objects and enter catch clauses, how noexcept and -fno-exceptions affect code generation, and what zero-cost really costs.draftexceptionsABIItaniumcode generation

Engineering practice

Physical design: headers and dependenciesHow to organize headers and source files so that a project both compiles and compiles fast: #include as textual replacement and its cost, include guards and #pragma once, self-contained headers, what forward declarations can and can't do, inline, static and unnamed namespaces in headers, explicit instantiation of templates, the dependency graph, cyclic dependencies and levelization, and modules as the way out.drafttoolchainheadersforward declarationsODRBuild speedHow to measure and shorten C++ build times: -ftime-trace to see where the time goes, the split between frontend and backend, the cost of standard headers and template instantiation, and how much precompiled headers, import std, extern template, unity builds, parallel builds, compiler caches, linkers, debug info and LTO each save or cost.drafttoolchainbuildcompile timeprecompiled headersLibrary boundaries: static libraries, shared libraries and ABIWhat makes up the boundary of a C++ library: what static and shared libraries contain, controlling exported symbols with visibility, which changes break binary compatibility without changing any symbol name, why a C interface is the most stable boundary, how exceptions and RTTI cross shared libraries, who allocates and who frees, and how install names, sonames and inline namespaces manage versions.drafttoolchainlibrariesshared librariesstatic librariesOrganizing a project with CMakeTarget-centric CMake: a library, an executable and tests; PRIVATE/PUBLIC/INTERFACE usage requirements; which compiler options the language standard and build type finally turn into; warnings, sanitizer presets, compile_commands.json; installing and exporting a library; building modules; and common anti-patterns.drafttoolchainCMakebuildODRManaging dependenciesHow to bring third-party libraries into a C++ project: why there is no common package format and why compile options must match, how two versions of the same library in one program cause an ODR violation, how system packages, FetchContent, vcpkg, Conan, submodules and vendoring each work, and pinning versions, verifying hashes and choosing by project type.drafttoolchaindependenciespackage managementODRChoosing an error-handling strategyHow a project reports and handles errors: first tell program bugs, recoverable environmental failures and exceptional situations apart, then choose between exceptions, error_code, std::expected and return codes, and deal with module boundaries, -fno-exceptions, error context, nodiscard, assertions, standard library hardening and contracts.drafterror handlingexceptionsexpectederror_codeDesigning and evolving APIsHow to design C++ interfaces that are hard to misuse and can evolve: parameter and return types that express ownership and nullability, const and ref-qualifiers, making illegal states unrepresentable, nodiscard, deprecated and noexcept as part of the contract, the overload-set and ADL risks library authors face, and changing an interface without breaking users, giving users feature-test macros, and documenting preconditions.draftAPIinterface designcompatibilitynoexceptTesting, sanitizers and fuzzingHow to test C++ code so that UB and data races get caught: a minimal test framework, compile-time tests and compile-failure tests, what ASan, LSan, UBSan and TSan each catch, what they cost and which can't be combined, standard library hardening in tests, a CI build matrix, fuzzing with libFuzzer, property-based testing, coverage with llvm-cov, and the same tests giving different results at -O0 and -O2.drafttoolchaintestingsanitizersASanCompiler warnings and static analysisFinding defects before running: what the standard requires compilers to diagnose and what it doesn't, which warnings -Wall, -Wextra and -pedantic actually enable, a recommended set of warning options and the errors each catches, -Werror and third-party headers, lifetime and thread-safety annotations, using clang-tidy, the Clang static analyzer and clang-format, and what static analysis can't do.drafttoolchainwarningsclang-tidystatic analysis

Debugging and diagnostics