Documentation
Run C++ Program

Running C++ Programs

A Hello World Program

Here is a simple C++ program that prints "Hello, World!" to the console:

hello.cpp
#include <iostream>
 
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

To compile and run this program, follow the steps below.

Save the Program

Save the program to a file named hello.cpp.

Compile the Program

To compile the program, you can use the following command:

sh
g++ hello.cpp -o hello

This command will compile the hello.cpp file and generate an executable file named hello.

Run the Executable

After compiling the program, you can run the executable by executing the following command:

sh
./hello

This will print Hello, World! to the console.

Compile multiple files

If you have multiple C++ files that you want to compile, you can use the following command:

Compile with GCC

To compile multiple C++ files with GCC, you can use the following command:

sh
g++ file1.cpp file2.cpp -o output

This command will compile file1.cpp and file2.cpp and generate an executable file named output.

Run the Executable

After compiling the files, you can run the executable by executing the following command:

sh
./output