Contact
Evil Genius SocietyHomeLearnForumLeaderboardSocialsMessages
← Back to module

Lesson 4 of 6

You'll type some form of this command thousands of times. It's worth understanding each piece.

g++ hello.cpp -o hello
  • g++ — the compiler driver. It runs all four stages from lesson 2.2 in order.
  • hello.cpp — your input. You can list several source files here.
  • -o hello — "output to a file named hello."

Leave out -o and you get a file called a.out, a convention inherited from 1970s Unix. Always pass -o; a.out tells you nothing about what it is.

Choosing the language standard

C++ gets a new revision every three years, and compilers must be told which one you want. This curriculum teaches C++20:

g++ -std=c++20 hello.cpp -o hello

Without -std, you get whatever the compiler defaults to, which varies by version. Features taught in this course — std::format, ranges, concepts — may simply not exist. Pass it every time.

Turning warnings on

By default the compiler stays quiet about code that is legal but almost certainly wrong. This is the single highest-value habit in this lesson:

g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
  • -Wall — "all" the common warnings (a historical name; it isn't literally all of them).
  • -Wextra — a further set that catches real bugs.

Here's a program that compiles cleanly with no flags, and is still wrong:

#include <iostream>

int main() {
    int total = 5;
    int typo_total = 0;
    std::cout << "Total: " << total << "\n";
    return 0;
}
Total: 5

Compile it with -Wall -Wextra and you're told typo_total is set but never used — a strong hint that you meant to use it somewhere and didn't. Without the flags, silence.

Some teams go further with -Werror, which turns every warning into an error and refuses to build. That's a good habit once you're comfortable; while learning, it can be more frustrating than useful.

The command you should actually use

For the rest of this course:

g++ -std=c++20 -Wall -Wextra main.cpp -o main

That's long to retype. Two ways to avoid it — a shell alias for quick experiments, or (much better, once your project has more than one file) a build system. Module 17 covers CMake, which is how real projects handle this.

A useful flag while learning

-g includes debug information in the executable, which is what lets GDB show you your source code and variable names instead of raw addresses (lesson 11.2):

g++ -std=c++20 -Wall -Wextra -g main.cpp -o main

It makes the binary bigger and doesn't slow it down. Keep it on while learning.

Recompiling

The compiler reads your source at the moment you run it. Edit the file and the running executable doesn't change; the file on disk doesn't change. You must recompile.

The loop is always the same:

  1. Edit and save the source.
  2. Run the g++ command.
  3. Run ./main.

If your changes don't seem to be taking effect, it's almost always step 1 (didn't save) or step 2 (didn't recompile) — the same trap as Module 1's exercise.

Try it

Compile a program without -o, and check what appears in the directory. Then compile the same program with -Wall -Wextra after adding an unused variable, and read the warning.

Checkpoint

  • g++ -std=c++20 -Wall -Wextra source.cpp -o output is your default command.
  • Without -o you get a.out; without -std you get an unpredictable language version.
  • -Wall -Wextra catches legal-but-wrong code that is otherwise silent.
  • -g adds debug info for GDB; keep it on while learning.
  • A warning still produces a program; an error does not.
  • Edit → save → recompile → run, every time.

Questions about this lesson

Log in to ask a question.