Compile and Run C++

Build Chemeketa project templates with Make. You can also run the compiler yourself to practice the complete command or diagnose a build problem.

Compiler commands by environment

EnvironmentCompiler command
Codespacesg++
Windowsg++
macOS with the required Homebrew GCCg++-15
Linuxg++
WSLg++

Compile one source file

Use the appropriate compiler command for your environment:

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

On macOS, replace g++ with g++-15.

Compile every .cpp implementation file that belongs to the program. Do not list header files as compiler inputs:

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

Run the program

Bash in Codespaces, macOS, Linux, or WSL:

./program.exe

PowerShell on Windows:

.\program.exe

Build a template with Make

From the project folder, use:

EnvironmentCommand
Windowsmingw32-make
Codespaces, macOS, Linux, or WSLmake

Template targets include:

make program.exe
make tests.exe
make clean

Not every template contains both executable targets. The Basic Project builds program.exe, the Unit Test Project builds tests.exe, and the Combo Project builds both.

To learn how Makefile rules, prerequisites, and targets work, continue to the Makefile Tutorial.

Common compiler options

  • -g includes information needed by the debugger.
  • -Wall -Wextra enables useful warnings.
  • -std=c++20 selects the course language standard.
  • -o NAME chooses the executable's filename.
  • -I FOLDER adds a folder to the header search path.
  • -c compiles without linking an executable.