Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 23Name in the book: Compiling Programs

Compile programs from source

Most software comes as packages, but sometimes you need a program that is not in the repositories, a newer version, or you want to change the code. Then you build it from source code. This chapter shows the classic steps with a small C program: get the source, look at it, ./configure, make, make install.

  • 8 Lessons
  • 11 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Build a program from source code

23.1

Know what compiling is

Compiling turns source code (text a person can read) into machine code (numbers the CPU runs).

  • Early programs were written in assembly language, very close to machine code. High-level languages like C are easier to write.
  • A compiler translates C into object code. A linker joins it with libraries (shared code) into a program.
  • Scripting languages (bash, Python, Perl, PHP) are not compiled first: an interpreter reads and runs them directly. They are slower, but faster to write.

23.2

Install the build tools

You need a compiler (gcc) and make. Distributions offer a group of build packages.

which gcc make
sudo dnf group install "development-tools"
sudo apt install build-essential
gcc --version

23.3

Get the source code

Source code is usually a compressed tarball from the project's website. Unpack it in a working directory.

mkdir -p ~/src
cd ~/src
wget https://ftp.gnu.org/gnu/diction/diction-1.11.tar.gz
tar xzf diction-1.11.tar.gz
ls
diction-1.11  diction-1.11.tar.gz

At work

Use tar tzf file.tar.gz | head before you extract an unknown tarball. A good tarball unpacks into one directory.

Note

Many projects are on Git hosts today: git clone URL gets the source.

23.4

Look at the source tree

A typical GNU source tree has the same files: documentation, the configure script, the Makefile template and the .c and .h files.

cd diction-1.11
ls
config.guess  diction.c  getopt.c  Makefile.in  README  style.c
config.h.in   diction.pot  getopt.h  misc.c     sentence.c  test
configure     diction.spec  INSTALL  misc.h     sentence.h
FileWhat it is
README, INSTALLRead these first: how to build
configureA script that checks your system
Makefile.inTemplate for the Makefile
*.cC source files
*.hHeader files: descriptions shared between source files
  • #include "getopt.h" in a .c file pulls in a header file. Headers like <stdio.h> come from the system (/usr/include).

Commands in this lesson

23.5

Run configure

./configure checks the system (compiler, libraries, tools) and writes a Makefile that fits it.

./configure
checking build system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
config.status: creating Makefile
config.status: creating config.h
  • It runs from the current directory, so it needs ./.
  • If something is missing, configure stops and says what. Install the -devel (Red Hat) or -dev (Debian) package of that library.
  • ./configure --prefix=$HOME/.local installs into your home directory later (no root needed).

23.6

Build with make

make reads the Makefile and runs the compiler. It only rebuilds what changed since the last build.

part of a Makefile
diction: diction.o sentence.o misc.o getopt.o getopt1.o
	$(CC) -o $@ $(LDFLAGS) diction.o sentence.o misc.o getopt.o getopt1.o $(LIBS)
 
diction.o: diction.c config.h getopt.h misc.h sentence.h
make
gcc -c -O2 diction.c
gcc -o diction diction.o sentence.o misc.o getopt.o getopt1.o
ls -l diction style
make
make: Nothing to be done for 'all'.
touch getopt.c
make
  • A target (diction) depends on files (diction.o …). If a dependency is newer than the target, make runs the command below it.
  • The command line in a Makefile must start with a tab, not spaces.
  • make -j$(nproc) builds with all CPU cores at the same time.

23.7

Install the program

make install copies the program, its man pages and data to the system, normally under /usr/local.

sudo make install
which diction
/usr/local/bin/diction
man diction
  • /usr/local is for software you build yourself. Package tools do not touch it.
  • Programs installed this way are not known to dnf or apt: you must update and remove them yourself (make uninstall if the project has it).

Watch out

Compile as a normal user. Use sudo only for make install.

23.8

Summary of the chapter

Get the source → read README → ./configure → make → sudo make install.

1What does ./configure do?

It checks the system for compilers, libraries and tools, and writes a Makefile for this system.

2Why does a second make say "Nothing to be done"?

Nothing changed since the last build; make only rebuilds targets whose dependencies are newer.

3Where does make install normally put programs?

Under /usr/local (for example /usr/local/bin).

Commands in this lesson

All commands in this chapter