CHAPTER 12 — PRESENTATION

Compiling Source Code

Master the full source compilation workflow: downloading tarballs, running ./configure, understanding Makefiles, compiling with gcc/make, installing, and managing library dependencies with ldconfig and pkg-config.

Slide 1 — Why Compile from Source?

When Package Managers Are Not Enough

Linux package managers (apt, dnf, pacman) are the preferred way to install software. They handle dependencies, security updates, and clean removal. However, there are valid reasons to compile from source:

TRADE-OFFS

Source-compiled software does NOT receive automatic security updates. You are responsible for monitoring for new versions and recompiling. This is why package managers are strongly preferred for production systems unless there is a specific compelling reason.

Slide 2 — The Compilation Pipeline

Six Steps from Source to Running Program

1

Get the Source Code

Download a tarball from the official site, or clone a git repository.

wget https://example.com/app-1.0.tar.gz git clone https://github.com/user/app
2

Extract and Explore

Extract the archive and read README, INSTALL, and NEWS files to understand build requirements.

tar xzf app-1.0.tar.gz && cd app-1.0
3

Run ./configure

The configure script checks your system for required libraries and generates a Makefile tailored to your environment.

./configure ./configure --prefix=/usr/local
4

Compile with make

Invokes the compiler (gcc) according to the Makefile rules. Binaries appear in the source directory.

make make -j$(nproc)
5

Install with make install

Copies binaries to system paths (/usr/local/bin). Installs man pages. Usually requires root.

sudo make install
6

Clean Up and Test

Remove source directory and tarball. Verify the installation.

which app app --version
Slide 3 — The ./configure Script

What configure Does and Key Options

The configure script is generated by autoconf. It performs a series of system checks: compiler availability, library headers, function availability, and system capabilities. The output is a Makefile and a config.h header file.

# Basic configure (uses default install prefix /usr/local) ./configure # Change install directory prefix ./configure --prefix=/opt/myapp # Enable optional features at compile time ./configure --enable-ssl --enable-debug # Disable features ./configure --disable-ipv6 # Specify library path if not in standard location ./configure --with-openssl=/usr/local/openssl # List all available configure options ./configure --help # If configure fails — missing library headers # Install development packages (headers): libname-dev or libname-devel sudo apt install libssl-dev # Debian/Ubuntu sudo dnf install openssl-devel # Fedora/RHEL
CONFIGURE FAILURE PATTERN

When ./configure fails with "library not found," the library package is installed but its development headers are missing. On Debian/Ubuntu, install the -dev variant of the package. On RHEL/Fedora, install the -devel variant. The runtime library and the development headers are separate packages.

Slide 4 — Makefiles and the make Command

Understanding Make Build Targets

# Standard Makefile targets: make # compile (default target: 'all') make -j4 # compile using 4 CPU cores (parallel) make -j$(nproc) # use all available CPU cores sudo make install # install to system paths make clean # remove compiled objects (keep Makefile) make distclean # remove everything including Makefile (full reset) make uninstall # remove installed files (if supported) make test # run built-in test suite (if available) # A minimal Makefile structure: CC = gcc CFLAGS = -Wall -O2 hello: hello.c $(CC) $(CFLAGS) -o hello hello.c clean: rm -f hello install: cp hello /usr/local/bin/

The -j flag enables parallel compilation — on a 4-core machine, make -j4 compiles four source files simultaneously, dramatically reducing build time for large projects.

Slide 5 — gcc, pkg-config, and ldconfig

Manual gcc Compilation and Library Management

# Install build tools (essential for compiling) sudo apt install build-essential # Debian/Ubuntu: gcc, make, libc-dev sudo dnf groupinstall "Development Tools" # Fedora/RHEL # Basic gcc compilation gcc hello.c -o hello # compile single C file gcc -Wall -O2 app.c -o app # with warnings and optimization gcc app.c -lm -o app # link math library gcc app.c -lssl -lcrypto -o app # link OpenSSL libraries # pkg-config: find correct compiler flags for installed libraries pkg-config --cflags --libs openssl # Output: -I/usr/include/openssl -lssl -lcrypto gcc app.c $(pkg-config --cflags --libs openssl) -o app # ldconfig: rebuild dynamic linker cache after installing libraries sudo ldconfig # scan /etc/ld.so.conf.d/ for new libs ldconfig -p # list all cached shared libraries ldd app # show shared library dependencies of a binary
Slide 6 — checkinstall: Make Removal Easy

checkinstall — Source to Package

checkinstall is a tool that intercepts make install and creates a proper distribution package (.deb, .rpm, or .tgz) instead of installing files directly. This makes the software trackable and removable by your package manager.

# Install checkinstall sudo apt install checkinstall # Debian/Ubuntu # Replace 'sudo make install' with checkinstall # After compiling (make), run: sudo checkinstall # checkinstall will ask for: # - Package name, version, description # - Creates a .deb file and installs it via dpkg # Now you can remove the compiled software cleanly: sudo apt remove myapp # Verify installation path after make install: which app # find where binary was installed # Expected: /usr/local/bin/app # View the installed manual page: man app # Remove source directory after successful install rm -rf app-1.0/ rm -f app-1.0.tar.gz
BEST PRACTICE SUMMARY

The complete safe workflow: (1) read README, (2) install build dependencies, (3) ./configure --prefix=/usr/local, (4) make -j$(nproc), (5) make test, (6) sudo checkinstall (preferred) or sudo make install, (7) update ldconfig if you installed shared libraries, (8) verify with which and --version, (9) clean up source directory.

Chapter 12 Complete

Mark this presentation complete to record your progress and unlock the quiz.

Progress saved. Head to the quiz to test your knowledge.