5.3.8 · HinglishBuild Systems & Toolchain

CMake — CMakeLists.txt, add_executable, add_library, target_link_libraries

1,889 words9 min readRead in English

5.3.8 · Coding › Build Systems & Toolchain


1. Core vocabulary


2. Ek minimal, complete project (piece by piece derive karo)


3. Libraries aur linking — iska dil

Figure — CMake — CMakeLists.txt, add_executable, add_library, target_link_libraries

4. Transitive dependency derivation (Forecast-then-Verify)


5. Common mistakes (Steel-man each)


6. Active recall

Recall Expand se pehle answer karo
  • CMake ke do build phases kya hain? → configure phir build.
  • Kaun sa command runnable vs linkable cheez banata hai? → add_executable vs add_library.
  • Tum apne header mein dependency expose kar rahe ho. PUBLIC, PRIVATE, ya INTERFACE? → PUBLIC.
  • Header-only library? → add_library(... INTERFACE) + INTERFACE link karo.
  • CMake link order kaise decide karta hai? → dependency graph se, automatically.
CMake ka role ek phrase mein
Ek meta build-system jo native build files generate karta hai; yeh compile nahi karta.
CMakeLists.txt mein pehla command kya hona chahiye
cmake_minimum_required(VERSION x.y).
Runnable program build karne ka command
add_executable(name sources...).
Linkable library build karne ka command
add_library(name [STATIC|SHARED|INTERFACE] sources...).
Targets ke beech dependency declare karne ka command
target_link_libraries(target <KEYWORD> deps...).
TLL mein PRIVATE ka matlab
Sirf meri implementation mein use hota hai; users tak propagate nahi hota.
TLL mein PUBLIC ka matlab
Mujhe bhi use hota hai AUR jo mujhe link kare unhe bhi propagate hota hai.
TLL mein INTERFACE ka matlab
Mujhe use nahi hota; sirf mere users tak propagate hota hai (header-only).
STATIC vs SHARED library
STATIC (.a/.lib) binary mein copy hoti hai; SHARED (.so/.dll) runtime par load hoti hai.
Out-of-source build commands
cmake -S . -B build phir cmake --build build.
target_include_directories ko include_directories se kyun prefer karo
Pehla target-scoped hai aur sahi se propagate karta hai; doosra har target mein leak karta hai.
Agar A, B ko PUBLIC link kare aur C, A ko link kare, toh kya C ko B dikhega
Haan — PUBLIC deps A se C tak transitive hote hain.
Recall Feynman: 12-saal ke bachche ko samjhao

Socho tum LEGO se build kar rahe ho. CMake instruction booklet writer hai, builder nahi. Tum use batate ho: "Mujhe ek spaceship (program) chahiye aur use engine block (library) chahiye." CMake exact step-by-step order likh deta hai taaki ek robot (compiler) ise kisi bhi table par build kar sake — tumhare desk par, tumhare dost ke, ek factory mein. Jab tum kehte ho ki engine block "apne saath wires laata hai jo poori spaceship ko chahiye," woh hai PUBLIC: woh wires share ho jaate hain. Agar wires engine ke andar chhupe rehte hain, woh hai PRIVATE. Tum sirf describe karte ho ki kya kisse connect hai; CMake cheezein bolne ka messy order handle karta hai.


Connections

  • Makefiles — woh low-level files jo CMake generate karta hai.
  • Ninja build system — ek faster generator backend (cmake -G Ninja).
  • Static vs Dynamic Linking — STATIC/SHARED ka link/runtime par actually kya matlab hai.
  • Compilation Pipeline preprocess-compile-assemble-link — TLL link stage control karta hai.
  • Header files and Include Guards — kyun include dir propagation matter karta hai.
  • Package Management find_package — external libs target kaise bante hain jise tum link kar sako.

Concept Map

read by

configure phase generates

build phase invokes

declares

via add_executable

via add_library

attach flags to

links

linked into

isolates junk from

CMakeLists.txt script

CMake meta build-system

Generated build files

Compiler

Target buildable unit

Executable

Library

target_* commands

target_link_libraries

Out-of-source build