Beginner · Getting started with C · topic 2 of 60 · 30 min
Compiling with gcc
Explanation
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
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| Flag | Meaning |
|---|---|
| -o name | Name of the output file (default a.out, or a.exe on Windows) |
| -std=c17 | Use the C17 standard (or -std=c11, -std=c23) |
| -Wall -Wextra | Turn on the common and extra warnings |
| -Werror | Treat warnings as errors (good in projects) |
| -g | Include debugging information (for gdb, module 11) |
| -O2 | Optimise for speed (release builds) |
| -lm | Link the math library (for sqrt, pow on Linux) |
The four stages
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 → executableThe 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
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=]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%.
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
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.
gcc -std=c17 -Wall -Wextra -Werror -O2 -g meter.c tariff.c -o meter
./meter && echo "exit status: $?"Practice (0/3 done)
Which command compiles calc.c into an executable called calc with warnings on?
The error "undefined reference to `sqrt'" comes from which stage, and what is the usual fix on Linux?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- The structure of a C programGetting started with C · same module
- Data types and their sizesGetting started with C · same module
- What C++ is and how compiling worksC++ Programming (modern C++ and DSA) · First steps: compiling, output, variables and input
- Splitting a program into filesThe preprocessor, multi-file programs and make
- Installing the JDK and your first programJava Programming (Core to Spring Boot) · Getting started with Java
- Bash scripts: variables, quoting, arguments and exit codesLinux and Red Hat (RHCSA to RHCE) · Automation basics: scheduling, archives and shell scripts
Questions people ask about this
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.
What does gcc -c file.c produce?Related
Answer: An object file (file.o) that is not yet linked
Compile only, no linking.
An ArithmeticException: / by zero appears. What kind of error is it?Related
Answer: Run-time exception
The program compiled; it failed while running.
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
- 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.
- Module 1 exam: Getting started with C · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Operators and control flow. 60 topics left in the course before the final exam.