C++ Installation and Setup

To write C++ you need a compiler, which changes your code to a program. The most used ones are GCC (g++), Clang and MSVC (Visual Studio).

Install the compiler

Windows

  • Visual Studio Community (free): choose “Desktop development with C++”. It includes the MSVC compiler.
  • Or install MSYS2 or MinGW-w64 to get g++.

macOS

xcode-select --install          # installs Clang with the command line tools

Linux

sudo apt update
sudo apt install g++ build-essential

Check the install

Terminal

$ g++ –version | head -1
g++ (GCC) 16.2.0

Compile and run

Save the program as hello.cpp:

#include <iostream>

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

$ g++ -std=c++23 -Wall -Wextra hello.cpp -o hello
$ ls
hello
hello.cpp
$ ./hello
Hello, C++!

PartMeaning
g++The compiler
-std=c++23Use the C++23 standard
-Wall -WextraShow more warnings. Always use them
hello.cppYour source file
-o helloName of the program to make

Code editors and IDEs

  • Visual Studio Code with the C/C++ extension (light, free).
  • CLion (JetBrains), Visual Studio and Code::Blocks.
  • For small tests, you can use the terminal and a text editor.
Note: Use -g to add debug information, and -O2 to make an optimized (fast) release build. Next: Your First C++ Program.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.