01 / 21
>_

Command-Line
Compilation

Learn how to compile and run C++ programs from the terminal using g++ and clang++: the foundation every developer needs.

C++

What Is Compilation?

Computers don't understand C++ directly. Your processor speaks machine code: raw binary instructions. Compilation is the process of translating your human-readable C++ source code into an executable file the computer can run.

The Build Pipeline

Source main.cpp Preprocess #include Compile + Link Executable a.out You write this CPU runs this

Why the Command Line?

Control
You choose every compiler flag and option: no hidden magic from an IDE.
Universal
Works on Linux, macOS, and Windows (WSL). Terminal skills transfer everywhere.
Foundation
Build systems like Make and CMake automate these same commands under the hood.

Meet g++ and clang++

g++ (GNU Compiler Collection) and clang++ (LLVM Clang) are the two most widely used C++ compilers. Both accept nearly identical command-line syntax for everyday use.

g++
The default on most Linux distributions. Part of GCC. Mature, highly optimizing.
clang++
Default on macOS. Known for clear error messages. Drop-in replacement for g++.

The Core Command

Terminal
$ g++ main.cpp
g++ main.cpp Compiler Source file produces a.out Executable

Compile vs. Run

Compile Run
Command g++ main.cpp ./a.out
What happens Translates source to machine code Executes the machine code
When errors appear Syntax errors, type errors Runtime errors, logic bugs
Output An executable file Your program's output

Step-by-Step Workflow

1
Write your code in a text editor and save it as a .cpp file.
2
Open a terminal and navigate to the folder containing your file with cd.
3
Compile with g++ main.cpp (or add -o name to choose the output name).
4
Fix errors if any are reported: read the line number and message.
5
Run the executable with ./a.out (or ./name if you used -o).

Essential Compiler Flags

Flag Purpose Example
-o name Set output filename g++ main.cpp -o hello
-Wall Enable all warnings g++ -Wall main.cpp
-std=c++17 Use C++17 standard g++ -std=c++17 main.cpp
-g Include debug info g++ -g main.cpp

Example 1: Hello World

hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
Terminal
$ g++ hello.cpp -o hello
$ ./hello
Hello, World!

Example 2: Warnings and Standards

age.cpp
#include <iostream>

int main() {
    int age;  // uninitialized!
    std::cout << "Age: " << age << std::endl;
    return 0;
}
Terminal
$ g++ -Wall -std=c++17 age.cpp -o age
age.cpp:5: warning: 'age' is used uninitialized

Example 3: Multiple Files

Real projects split code across files. You can list them all in one command:

Terminal
$ g++ -Wall -std=c++17 main.cpp utils.cpp -o app
$ ./app
main.cpp utils.cpp g++ app

Reading Compiler Errors

Error Output
main.cpp:6:18: error: expected ';' after expression
    cout << "Hi"
                 ^
                 ;
main.cpp : 6 : 18 : error: expected ';' File Line Column Description

Why ./ Before the Executable?

On Unix-based systems, the shell only searches directories in your PATH for commands. Your current folder is usually not in the PATH. The ./ prefix explicitly says "look in the current directory."

Terminal
$ a.out
command not found: a.out

$ ./a.out
Hello, World!

Common Mistakes

✕ Forgetting ./
Typing a.out instead of ./a.out gives "command not found."
✕ Wrong directory
Running g++ when your terminal is not in the same folder as the .cpp file.
✕ Running old code
Editing the source but forgetting to recompile before running again.
✕ Skipping warnings
Not using -Wall hides bugs that the compiler could have caught.

Quick Reference Cheat Sheet

Task Command
Compile (default name) g++ main.cpp
Compile (custom name) g++ main.cpp -o program
Compile with warnings g++ -Wall main.cpp -o program
Compile C++17 g++ -std=c++17 -Wall main.cpp -o program
Run ./program
Multiple files g++ file1.cpp file2.cpp -o app

Question 1 of 5

What is the default name of the executable produced by g++ main.cpp?

Question 2 of 5

Which flag tells the compiler to enable all common warnings?

Question 3 of 5

You type a.out and get "command not found." What is the most likely fix?

Question 4 of 5

You edit main.cpp and run ./program but see the old output. What step did you forget?

Question 5 of 5

Which command compiles two source files into an executable named app?