Hello World!
Let’s start by running your first C++ program.
Do not worry about understanding the code yet. The goal is just to get things running.
The first thing to do is install an IDE. An IDE is a type of program you use to write code. There are many different IDEs available. We will show you how to work with one of them, called Code::Blocks, but feel free to experiment and find the one that suits you best.
Step 1: Install Code::Blocks
On Windows
- Go to the Code::Blocks downloads page.
- Download the installer named
codeblocks-25.03mingw-setup.exe(possibly there is a newer version, so instead of 25.03 there will be another number).- This version includes the MinGW compiler, so you do not need to install a compiler separately.
- Run the installer and keep the default options.
On macOS
Code::Blocks for macOS is older and does not include a compiler. It still works for simple projects, but you must install the compiler first.
- Install Apple Command Line Tools (this provides
clang):- Open Terminal and run:
xcode-select --install
- Open Terminal and run:
- Download
CodeBlocks-25.03_macOS-11.7_x64-wx3.2.6.dmgfrom Code::Blocks downloads page and install it (possibly there is a newer version, so instead of 25.03 there will be another number). - If Code::Blocks cannot find the compiler on first run, go to Settings -> Compiler and select Clang if available.
If Code::Blocks feels rough (especially on macOS), you can use CLion or VS Code, or any other IDE. See the end of this chapter for some details.
Step 2: Create a new project
- Open Code::Blocks.
- Go to File -> New -> Project -> Console application.
- Choose C++, click Next.
- Enter a project title, for example
HelloWorld. - Pick a folder for the project and click Finish.
Code::Blocks creates a project with a file named main.cpp inside.
Step 3: Write your first program
Open main.cpp and replace everything with:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}Step 4: Build and run
- Click Build and run (green triangle) or press F9.
- You should see:
Hello, World!
Great, you just ran your first C++ program!
What actually happened (short version)
When you click Build, Code::Blocks calls a compiler (MinGW on Windows, Clang on macOS). The compiler translates your code into a binary (an executable file). On Windows the file usually ends with .exe. This is the file your computer can run directly.
You will learn more about compilers, linkers, and build settings later. For now, this is enough.
Other IDE options
There are many IDEs and editors that work for C++. We also recommend:
- CLion
- VS Code
These might take a little more work to set up, but they are also more powerful! On Windows and macOS, you will need to install a compiler. Linux already includes the GCC compiler. If you are using VS Code, you will also need to install a C++ extension.
There are also some online IDEs, which are simple to start with but not a good choice in the long term.
You can use any of these later. For now, Code::Blocks is a simple starting point.