gcc(1)

Development tool

The GNU compiler: compile and link a C program, turn on warnings, keep debugging information, pick the language standard, link a library.

Synopsis
gcc [options] file.c... [-o output]

Description

gcc turns your .c files into an executable. Behind a single command, it runs the preprocessor, the compiler proper, the assembler and the linker. With no options, it compiles quietly, or nearly so, without optimization, in the default dialect of its version (gnu23 since GCC 15), and names the result a.out. Options are there to make it speak up about likely mistakes and to prepare the program for debugging.

Essential options

OptionEffect
-o filenames the executable or the file produced
-Wall -Wextraturns on the useful warnings; use them on every build
-Werrorturns every warning into an error, and no executable is produced
-gkeeps names and line numbers for gdb and Valgrind
-O0, -O2no optimization (the default, best for debugging), or optimized
-std=c17, -std=c23picks the version of the language
-ccompiles without linking and produces a .o
-I diradds a directory to search for headers
-L diradds a directory to search for libraries
-lnamelinks the library libname, for example -lm for the functions in <math.h>
-fsanitize=addressadds AddressSanitizer, which stops the program at the first invalid memory access

Example

A program in two files, with the header stored in include/. Compile each .c on its own, then link the two object files with the math library:

Terminal
$ gcc -Wall -Wextra -Werror -g -Iinclude -c geo.c -o geo.o$ gcc -Wall -Wextra -Werror -g -Iinclude -c main.c -o main.o$ gcc main.o geo.o -o distance -lm$ ./distance5.0

Keeping -c separate from linking lets you recompile only the file that changed, which is what make does. For a small program, one command is enough: gcc -Wall -Wextra -g -Iinclude main.c geo.c -o distance -lm.

Without -Iinclude, the preprocessor cannot find the header and everything stops there:

Terminal
$ gcc -Wall -Wextra -g -c main.c -o main.omain.c:2:10: fatal error: geo.h: No such file or directory    2 | #include "geo.h"      |          ^~~~~~~compilation terminated.

Pitfalls

  • Without -Wall -Wextra, gcc lets a forgotten return or an = in place of == slip through. The most common messages are explained in GCC warnings explained.

  • -l options go after the files that need them: the linker reads the command line from left to right. The typical error is undefined reference to `sqrt', covered in undefined reference to.

  • -g with -O2 produces a binary where some variables show up as <optimized out> in gdb. Debug at -O0.

  • The other way around, some warnings, such as a variable read before it was initialized, only appear with -O2. Build with both from time to time.

  • GCC 15 compiles as C23 by default, which breaks old code (typedef int bool;, f() declarations). See GCC 15 and C23 by default.

  • -fsanitize=address needs the libasan library, which the Alpine image of the Grademe exercises does not have: linking fails there with cannot find -lasan. Valgrind, on the other hand, is installed.

C

fix-the-warnings

Clear the three warnings that -Wextra -Werror turns into errors in clamp.c, then write the branch clamp is missing.

Open the exercise

Now write it yourself.