Running C++ Programs
A Hello World Program
Here is a simple C++ program that prints "Hello, World!" to the console:
#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:
g++ hello.cpp -o helloThis 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:
./helloThis 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:
g++ file1.cpp file2.cpp -o outputThis 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:
./output