Unit 1 of 9 · about 4 hours · explain

Compiling, linking, loading, ABI and C linkage

A native extension can compile successfully yet fail to link or import. Separating these stages lets you repair the boundary rather than change unrelated source code.

You'll be able to

  • Trace a source file through preprocessing, compilation, assembly, linking and loading, naming each artifact.
  • Diagnose a missing declaration, missing definition or missing shared library at the responsible stage.
  • Explain which ABI and ownership agreements a C-linkage boundary still needs.

Covers

Preprocess, compile, assemble, link, load and symbolsWarnings, debug symbols, optimization and ABI compatibilityC linkage and stable foreign-function boundaries
1

Read

In this order, only the parts listed

0 of 4 done

  1. Compilation ProcessAlgoMaster C++

    Recap of the Pipeline; Stage 1 through Stage 4; Putting It Together: A Two-File Build; Mapping Errors to Stages

    Use the artifacts and error examples. Do not adopt the lesson's unmeasured optimization speedup ranges or its mention of trigraph translation: trigraphs were removed in C++17.

  2. Language linkagecppreference

    Explanation and Notes, including the __cplusplus conditional header example

    Skip compiler-specific function-type overload disputes.

  3. Cross-compiler interfacesC++ Core Guidelines

    I.26: If you want a cross-compiler ABI, use a C-style subset

  4. Linux dynamic linker/loader`ld.so`: dynamic linker/loader

    DESCRIPTION, including dependency search order and Dynamic string tokens; ENVIRONMENT entry LD_DEBUG

    Stop before legacy hardware-capability directories and auditing.

2

Focus on

  • A declaration lets a translation unit type-check a call. A definition supplies the implementation; an object file can still contain unresolved symbols.
  • Use -Wall -Wextra -Wpedantic and -g for development. Compare -O0 and -O2 artifacts without treating optimization as a correctness fix or a promised speedup.
  • ABI covers calling conventions, representation and binary compatibility. extern C linkage does not make STL objects, exceptions or allocators portable across arbitrary binaries.
  • The Linux loader searches for shared objects at runtime. A link-time -L directory is not automatically a runtime search path.
Common mistakes · 3
  • Including a header is not the same as linking the implementation that its declarations require.
  • An unmangled C symbol is not a complete stable ABI. Agree on widths, buffer lengths, ownership, error codes and compatible platform conventions.
  • Debug symbols and optimization are independent choices. Optimized code can have debug information while some source variables are optimized out.
3

Try it yourself

Closed-book, before checking anything

With g++ 15.2, -std=c++20 -O0 -g on Linux x86-64, define extern "C" int inc(int x) { return x + 1; } in one translation unit and declare it in another whose main returns inc(41) == 42 ? 0 : 1. Produce preprocessed text with -E, assembly with -S and objects with -c. Inspect nm: the defining object has T inc and the caller U inc. Linking only the caller fails with undefined reference to inc; linking both objects succeeds and exits zero. Compare -O2 assembly, recording the flags rather than promising specific instructions.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why can -c succeed when a called function has no definition?Answer

    The declaration suffices for compilation. The object records an unresolved symbol for the linker to resolve.

  2. 2.An executable links but reports a missing .so when started. Which stage needs investigation?Answer

    Loading. Inspect the dependency name and runtime search paths, not just the source include path.

  3. 3.Does extern C make a function returning std::vector<int> safe for an arbitrary C caller?Answer

    No. It changes language linkage, not the vector representation, C type system or allocator ownership contract.

You're done when

You reproduce the two-object link and correctly classify a missing header, unresolved symbol and missing runtime library, with a written C-boundary ownership contract.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 6 more in the section
  • AlgoMaster C++AlgoMaster · primary

    Use the sections on compilation, pointers and references, memory, RAII and smart pointers, copy and move, STL containers, templates, errors, concurrency and paired exercises. Skip competitive-programming breadth already covered in Algorithms.

  • C++ Core GuidelinesStandard C++ Foundation community · required

    Use the Philosophy, Interfaces, Resource management, Expressions, Containers and concurrency rule groups to review ownership and safety decisions in the native component.

  • cppreferencecppreference community · support

    Consult language lifetime, object model, value categories, memory model, atomics, containers and iterator invalidation while reading or modifying code.

  • `ld.so`: dynamic linker/loaderLinux man-pages project · support

    Read DESCRIPTION, Dynamic string tokens and LD_DEBUG to distinguish link-time symbols from runtime shared-library discovery on Linux.

Also in this section

  • Clang AddressSanitizer and UndefinedBehaviorSanitizerLLVM · required

    Compile seeded memory defects with AddressSanitizer and UndefinedBehaviorSanitizer, preserve the reports, repair the root cause and keep regression tests.

  • pybind11 documentationpybind11 project · required

    Follow First steps and NumPy support to expose one small native function with explicit shape, dtype, stride, ownership, GIL and exception behavior.

  • CMake tutorialCMake · support

    Use the introductory steps for targets, libraries, testing and install rules. Produce one clean debug build and one optimized build without copied one-off commands.

  • Algorithmica - AoS and SoASergey Slotin · support

    Read the AoS and SoA experiment for a measured case where keeping each record together wins, the counterpart to a single-field scan.

  • Memory Layout TransformationsIntel · support

    Read Overview and Topics through AOS Form and SOA Form for field-wise access and locality trade-offs. Skip Intel array-notation syntax and the LIBOR example.

  • GDB manual: BacktracesGNU Project · support

    Use section 8.2 for frame numbers, bt, bt -full and optimized-out arguments when inspecting a native failure.