🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME 01POSTS 02COURSES 03ELECTRİCAL_ENERGY 04AUTOMOTİVE_ENGİNEERİNG 05TOOLS 06LAB 07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

Setup: g++, MSVC and your first build

Install a C++ compiler on Windows, Linux and macOS, and learn the path from a source file to a running program.

C++ is a compiled language. The computer cannot run the text file you write (the source code) directly; first a tool called a compiler translates it into machine code. In languages like Python an interpreter runs the code line by line, whereas in C++ you compile first and then run the resulting .exe file. That is why our first job is to install a compiler.

Which compiler should you choose?

There are three major compilers. They all understand the same language; the differences are in installation and in their error messages.

CompilerPlatformCommand
g++ (GCC)Linux, Windows (MSYS2), macOSg++
MSVC (Visual Studio)Windowscl
clang++macOS, Linux, Windowsclang++

Windows: with g++ (MSYS2)

Download and install MSYS2 from msys2.org. When the installation finishes, open the "MSYS2 UCRT64" shortcut and install the compiler with this command:

pacman -S mingw-w64-ucrt-x86_64-gcc

Then add the C:\msys64\ucrt64\bin folder to the Windows PATH environment variable so you can type g++ from any terminal.

Windows: with MSVC

When installing Visual Studio Community, or just "Build Tools for Visual Studio", tick the Desktop development with C++ workload. Then open "Developer Command Prompt" from the Start menu; the cl command is ready to use there.

Linux and macOS

# Debian / Ubuntu
sudo apt install build-essential

# macOS (Xcode command line tools)
xcode-select --install

Your first build

Create a folder and write a file named hello.cpp inside it. Don't try to understand every line yet; we will explain them all in the next lesson.

#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    return 0;
}

Compile and run it with g++:

g++ -std=c++17 -Wall -Wextra -o hello hello.cpp
./hello

The MSVC equivalent is:

cl /EHsc /std:c++17 /W4 hello.cpp
hello.exe
Hello, C++!

What do these flags do?

  • -std=c++17: tells the compiler which version of the language to use. C++17 is enough for this course.
  • -Wall -Wextra: turns on compiler warnings. A warning is very often the bug itself; never switch them off.
  • -o hello: the name of the output file. If you leave it out, you get a.out on Linux and a.exe on Windows.

What happens behind the scenes?

It may look like a single command, but building happens in three stages. The preprocessor expands the #include lines and merges the files. The compiler turns each .cpp file into object code. The linker collects the object files and libraries into a single executable. When you get an error message, checking which stage it came from speeds up the fix: messages like "undefined reference" come from the linker, while messages like "expected ';'" come from the compiler.

Engineer's note: in embedded systems (STM32, the Arduino family and so on) the program does not run on your own computer but on the target board. For that you use a cross-compiler, for example arm-none-eabi-g++ for ARM Cortex-M. The language is the same; only the compiler changes to match the target processor. What you learn in this course carries over there too.

If non-ASCII characters (such as accented letters) look garbled in the Windows terminal, run the chcp 65001 command first. Also save your source file as UTF-8. In the early lessons we will stick to ASCII characters in program output.

Try it yourself

  1. Install the compiler and check that the g++ --version (or cl) command works.
  2. Compile and run hello.cpp.
  3. Replace the text in the output with your own name and compile again.
  4. Delete the semicolon at the end of the line and read the error message the compiler gives you. Which line does the error point to?

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. Which tool translates C++ source code into machine code before it runs?
2. At which stage of the build does an “undefined reference” error usually appear?
3. Which of these are the g++ flags that turn on warnings?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.