Hello, C++: Your First Program
Set up your C++ compiler and write a classic Hello, World program — then compile and run it from the terminal.
This is the first post in a series about basic C++. Every serious C++ journey starts with one small program that prints a line of text.
Why C++?#
C++ is a compiled, statically typed, general-purpose language that gives you direct control over memory while staying expressive. It powers operating systems, game engines, browsers, databases, and high-performance applications. Learning it teaches you how computers actually work.
Install a compiler#
To run C++ you need a compiler. On Linux or Windows with WSL, install GCC:
sudo apt install g++ # Debian / UbuntubashOn macOS, install Xcode Command Line Tools:
xcode-select --installbashVerify it works:
g++ --versionbashYour first program#
Create a file named hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
return 0;
}cppCompile and run#
g++ -std=c++17 -Wall -o hello hello.cpp
./hellobashOutput:
Hello, C++!textWhat each part does#
#include <iostream>— brings in the input/output stream library sostd::coutworks.int main()— every C++ program starts executing frommain. It must exist exactly once.std::cout <<— prints text to the console.\nis a newline.return 0;— signals that the program finished successfully.-std=c++17— uses the C++17 standard;-Wallenables useful warnings.
Try changing the message, adding a second std::cout line, or using std::endl. Then compile again. The cycle of edit, compile, run, and observe is how you learn C++.
Conclusion#
You have compiled and run your first C++ program. Next in this series: variables and data types.