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.
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
| Option | Effect |
|---|---|
-o file | names the executable or the file produced |
-Wall -Wextra | turns on the useful warnings; use them on every build |
-Werror | turns every warning into an error, and no executable is produced |
-g | keeps names and line numbers for gdb and Valgrind |
-O0, -O2 | no optimization (the default, best for debugging), or optimized |
-std=c17, -std=c23 | picks the version of the language |
-c | compiles without linking and produces a .o |
-I dir | adds a directory to search for headers |
-L dir | adds a directory to search for libraries |
-lname | links the library libname, for example -lm for the functions in <math.h> |
-fsanitize=address | adds 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:
$ 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.0Keeping -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:
$ 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 forgottenreturnor an=in place of==slip through. The most common messages are explained in GCC warnings explained.-loptions go after the files that need them: the linker reads the command line from left to right. The typical error isundefined reference to `sqrt', covered in undefined reference to.-gwith-O2produces 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=addressneeds thelibasanlibrary, which the Alpine image of the Grademe exercises does not have: linking fails there withcannot find -lasan. Valgrind, on the other hand, is installed.
fix-the-warnings
Clear the three warnings that -Wextra -Werror turns into errors in clamp.c, then write the branch clamp is missing.