Skip to content

Beginner · Getting started with C · topic 2 of 60 · 30 min

Compiling with gcc

Explanation

gcc turns a .c file into an executable in four stages: preprocess, compile, assemble and link. The everyday command is gcc -std=c17 -Wall -Wextra -g hello.c -o hello, then ./hello to run it. Warnings point to real bugs, so read them and fix them; -Werror turns them into errors.

Installing a compiler

  • Linux (Ubuntu/Debian): sudo apt install build-essential (gives gcc, make and libraries).
  • Windows: install MSYS2 and its MinGW-w64 gcc package, or use WSL (Windows Subsystem for Linux) and install gcc inside Ubuntu. WSL is recommended because tools like valgrind (module 11) work there.
  • macOS: run xcode-select --install; the "gcc" command there is actually clang, which accepts the same common options.

Check the install with gcc --version. Any editor works; VS Code with the C/C++ extension is a popular free choice.

Compile and run

bash
gcc hello.c -o hello                          # compile hello.c into an executable named hello
./hello                                       # run it (Linux, macOS, WSL)
hello.exe                                     # run it (Windows cmd, MinGW)

gcc -std=c17 -Wall -Wextra -g hello.c -o hello   # the recommended everyday command
FlagMeaning
-o nameName of the output file (default a.out, or a.exe on Windows)
-std=c17Use the C17 standard (or -std=c11, -std=c23)
-Wall -WextraTurn on the common and extra warnings
-WerrorTreat warnings as errors (good in projects)
-gInclude debugging information (for gdb, module 11)
-O2Optimise for speed (release builds)
-lmLink the math library (for sqrt, pow on Linux)

The four stages

bash
gcc -E hello.c -o hello.i      # 1. preprocess: expand #include and #define (text)
gcc -S hello.i -o hello.s      # 2. compile: C → assembly language
gcc -c hello.s -o hello.o      # 3. assemble: assembly → machine code object file
gcc hello.o -o hello           # 4. link: join object files + libraries → executable
gcc normally does all four in one go.

The linker matters because your program calls functions you did not write, like printf. Their machine code lives in the C standard library; the linker connects your calls to it. "undefined reference to …" errors come from this last stage.

Errors and warnings

output
hello.c: In function 'main':
hello.c:5:5: error: expected ';' before 'return'
hello.c:4:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
file:line:column, then the message. Errors stop the build; warnings do not, but are usually bugs.
Careful: Never ignore warnings in C. A warning like "format %d expects int but argument is double" or "variable may be used uninitialised" usually means the program will print garbage or crash. Compile with -Wall -Wextra from day one and keep the output clean.

Exit status

When main returns, the number is the program's exit status: 0 means success, anything else means failure. Scripts and other programs check it: on Linux run echo $? right after your program; on Windows cmd, echo %ERRORLEVEL%.

Think of it like this: Compiling is like a print shop job: the preprocessor collects all the pages (includes), the compiler typesets them (assembly), the assembler makes printing plates (object files), and the linker binds your pages with standard pages from the library into one finished book (the executable).
Tip: Create a shell alias or a tiny Makefile (module 8) so you never forget the warning flags: alias cc17='gcc -std=c17 -Wall -Wextra -g'.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Compiling with gcc · 1/6

Compiling with gcc

In this lesson you will compile and run C programs with gcc.

Prefer a video by a teacher? Find free videos on “gcc compile c program tutorial wall wextra compilation stages” on YouTube ↗

In real life: A build command in a firmware team

An embedded team building firmware for smart energy meters compiles with strict warnings and treats every warning as an error, because a missed warning on a device in thousands of homes is very expensive to fix later.

bash
gcc -std=c17 -Wall -Wextra -Werror -O2 -g meter.c tariff.c -o meter
./meter && echo "exit status: $?"

Practice (0/3 done)

Task 1 · Question

Which command compiles calc.c into an executable called calc with warnings on?

Task 2 · Question

The error "undefined reference to `sqrt'" comes from which stage, and what is the usual fix on Linux?

Task 3 · Practice

Install gcc, write hello.c that prints your name, and compile it with gcc -std=c17 -Wall -Wextra -g hello.c -o hello. Run it and check the exit status. Then run gcc -E, -S and -c separately and open the .i and .s files to see each stage.

Quick quiz

Question 1

1.What does -g add?

Question 2

2.Which stage expands #include and #define?

Question 3

3.What does an exit status of 0 mean?

Question 4

4.What does -Werror do?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does -g add?

Answer: Debugging information for gdb

Needed to see source lines in the debugger.

Which stage expands #include and #define?

Answer: Preprocessing

gcc -E shows the preprocessor output.

What does an exit status of 0 mean?

Answer: Success

Non-zero means failure.

What does -Werror do?

Answer: Turns warnings into errors

The build fails on any warning.

Why add -Wall -Wextra?Related

Answer: To get warnings about code that is legal but probably wrong

Warnings catch likely bugs early, such as unused variables or suspicious comparisons.

From What C++ is and how compiling works

What does gcc -c file.c produce?Related

Answer: An object file (file.o) that is not yet linked

Compile only, no linking.

From Splitting a program into files

An ArithmeticException: / by zero appears. What kind of error is it?Related

Answer: Run-time exception

The program compiled; it failed while running.

From Installing the JDK and your first program

What exit code means success?Related

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Getting started with C” before its exam: The structure of a C program, Data types and their sizes, Input and output: printf and scanf.
  2. Module 1 exam: Getting started with C · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Operators and control flow. 60 topics left in the course before the final exam.

← Previous: What C is and why it still matters