"C++ Program in Terminal: How to Run & Compile"

Montreal Jun 10, 2026

Running C++ Programs in the Terminal: A Comprehensive Guide

an image of a computer screen that is running
an image of a computer screen that is running

C++ is a powerful programming language widely used for system programming, game development, and high-performance applications. To harness its full potential, understanding how to run C++ programs in the terminal is crucial. This guide will walk you through the process, from compiling your code to executing it in the terminal.

Bubble sort algorithm in C++
Bubble sort algorithm in C++

Setting Up Your Environment

Before you start, ensure you have the necessary tools installed. You'll need a C++ compiler like GNU Compiler Collection (GCC) or Clang, and a text editor or Integrated Development Environment (IDE) for writing your code.

my custom script at terminal startup
my custom script at terminal startup
  • On Ubuntu/Debian: Install GCC using `sudo apt install g++`.
  • On macOS: Install Homebrew and then GCC using `brew install gcc`.
  • On Windows: Install MinGW or use an IDE like Visual Studio or Code::Blocks.

Writing Your First C++ Program

Getting Started with the Linux Terminal
Getting Started with the Linux Terminal

Let's start with a simple "Hello, World!" program to get you familiar with the process.

// hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Compiling Your C++ Program

After writing your code, you'll need to compile it into an executable file. Here's how:

How to Open Spreadsheets In The Terminal - Make Tech Easier
How to Open Spreadsheets In The Terminal - Make Tech Easier
  • Using GCC: Open your terminal, navigate to your project directory, and run `g++ hello.cpp -o hello`.
  • Using Clang: Run `clang++ hello.cpp -o hello`.

The `-o` flag specifies the output file name. In this case, `hello` is the executable file that will be created.

Running Your C++ Program in the Terminal

Anyone having trouble accessing Twitter with Claro broadband? I can't take it anymore 🤯
Anyone having trouble accessing Twitter with Claro broadband? I can't take it anymore 🤯

Now that you've compiled your program, it's time to run it. Simply type `./hello` (on Unix-based systems) or `hello.exe` (on Windows) in your terminal and press Enter.

You should see:

an old television with green lines on the screen
an old television with green lines on the screen
Terminal Multiplexers Explained, and Why You'd Use One
Terminal Multiplexers Explained, and Why You'd Use One
Berkeley Graphics on X
Berkeley Graphics on X
a printable training plan for cross - country runners, with instructions to help them learn how
a printable training plan for cross - country runners, with instructions to help them learn how
Ver Star Wars IV en tu terminal #windows #fyp #javascript #programacion #python #hacks #aprendecont
Ver Star Wars IV en tu terminal #windows #fyp #javascript #programacion #python #hacks #aprendecont
an iphone screen showing the time and workouts for each person in this run map
an iphone screen showing the time and workouts for each person in this run map
Choosing Linux Terminal Emulators – Discussion and List
Choosing Linux Terminal Emulators – Discussion and List
Conditional Testing in Bash: if, then, else, elif
Conditional Testing in Bash: if, then, else, elif
the ghosty config logo on a black background with text that reads, customizing your terminal
the ghosty config logo on a black background with text that reads, customizing your terminal
an image of a circular object that looks like it is spinning in the air and has purple
an image of a circular object that looks like it is spinning in the air and has purple
Terminal 00
Terminal 00
an info sheet showing the benefits of running in 5k and 10k miles per hour
an info sheet showing the benefits of running in 5k and 10k miles per hour
6 Fun and Cool Mac Terminal Commands to Try
6 Fun and Cool Mac Terminal Commands to Try
a man running down the road in front of a sign that says pace run club
a man running down the road in front of a sign that says pace run club
Programming for Problem Solving Unit 4 Cheat Sheet | Arrays & Strings in C | AKTU Notes
Programming for Problem Solving Unit 4 Cheat Sheet | Arrays & Strings in C | AKTU Notes
the road map for the race is shown in red and yellow, as well as an arrow
the road map for the race is shown in red and yellow, as well as an arrow
Manage Your Linux System Resources With These 7 Terminal Commands
Manage Your Linux System Resources With These 7 Terminal Commands
an image of a computer screen with numbers and lines on the screen, all in different colors
an image of a computer screen with numbers and lines on the screen, all in different colors
a man is running on the beach with an info board above him that shows how to run
a man is running on the beach with an info board above him that shows how to run
people walking down the road with text overlay that reads 8 - week beginner training program to run for 30 minutes
people walking down the road with text overlay that reads 8 - week beginner training program to run for 30 minutes

Hello, World!

Debugging Your C++ Program

When things go wrong, debugging is essential. Both GCC and Clang support debugging flags. Here's how to compile your program with debugging symbols:

  • Using GCC: Run `g++ -g hello.cpp -o hello`.
  • Using Clang: Run `clang++ -g hello.cpp -o hello`.

To use a debugger like GDB, run `gdb hello` and follow the prompts.

Compiling and Running C++ Programs with Arguments

Sometimes, you'll want to pass arguments to your program. Here's how to modify your "Hello, World!" program to accept an argument:

// hello_args.cpp
#include <iostream>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        std::cout << "Hello, " << argv[1] << "!" << std::endl;
    } else {
        std::cout << "Hello, World!" << std::endl;
    }
    return 0;
}

Compile and run this program with an argument like so: `./hello_args World`. You should see:

Hello, World!