DEV Community

Cover image for Guide to Compiling C++: Making Your Code Run!

Guide to Compiling C++: Making Your Code Run!

Welcome. Let’s demystify compiling without making it a whole thing. If you can run a couple terminal commands, you can compile C++.

What compiling is

Your C++ file is readable to you. Your computer wants machine code. Compiling is the translation step that turns your .cpp into something your OS can execute.

In practice, this is you telling a compiler:
β€œTake these source files and give me a runnable program.”

The compiler you will actually use: g++

g++ is the common C++ compiler command on Linux and macOS (and on Windows if you are using something like MinGW or WSL).

General shape:

g++ [flags] [source files] -o [output_name]
Enter fullscreen mode Exit fullscreen mode

Flags you will see a lot

  • -Wall
    Show helpful warnings. Use it. Future you will thank you.

  • -O2
    Optimizes the build (faster program, sometimes slower compile).

  • -std=c++20 (or c++17)
    Picks the C++ language standard.

Example:

g++ -Wall -O2 -std=c++20 main.cpp -o app
Enter fullscreen mode Exit fullscreen mode

Output names, because this trips everyone once

The -o flag sets the name of the program you are creating.

g++ main.cpp -o app
Enter fullscreen mode Exit fullscreen mode
  • app is now your executable.
  • Run it like this:
./app
Enter fullscreen mode Exit fullscreen mode

Quick correction on file extensions

If you do not use -o, g++ does not create a .o file by default. It creates an executable named a.out:

g++ main.cpp
./a.out
Enter fullscreen mode Exit fullscreen mode

A .o file is an object file, and you usually only get those when you compile with -c:

g++ -c main.cpp -o main.o
Enter fullscreen mode Exit fullscreen mode

That is useful for multi file projects, but you can ignore it for your first few builds.

A step by step example

  1. Go to the folder with your code:
cd directory_name
Enter fullscreen mode Exit fullscreen mode
  1. Compile:
g++ -Wall -std=c++20 filename.cpp -o filename
Enter fullscreen mode Exit fullscreen mode
  1. Run:
./filename
Enter fullscreen mode Exit fullscreen mode

Compiling with tests (GoogleTest)

If you are linking GoogleTest manually, a simple pattern looks like this:

g++ -Wall -std=c++20 main.cpp tests.cpp gtest_main.a -pthread -o tests
Enter fullscreen mode Exit fullscreen mode

Notes:

  • -pthread is commonly needed for GoogleTest.
  • gtest_main.a provides a default main() for running tests.
  • Depending on your setup, you might link -lgtest -lgtest_main instead of using a .a file. It varies by install method.

Wrap

Compiling is just turning your code into something runnable. The terminal commands look scary exactly one time, then they are just part of your muscle memory.

If you want, paste the exact command you are using and the error you get. I can translate it into plain English and tell you the one line fix.

For more tutorials, visit https://nessakodo.com

Love,
NESSA KODO

Top comments (0)