Skip to content

Environment_setup

In this chapter, we’ll walk you through setting up your C++ development environment. Whether you’re using Windows, macOS, or Linux, we’ll cover the best options for getting started with C++ development.

There are three main approaches to setting up a C++ development environment:

  1. IDE (Integrated Development Environment): All-in-one solution with editor, compiler, debugger
  2. Code Editor + Compiler: Lightweight editor with external compiler
  3. Online Compilers: No installation required

Let’s explore each option.

Option 1: Integrated Development Environments (IDEs)

Section titled “Option 1: Integrated Development Environments (IDEs)”

Visual Studio is the most feature-rich IDE for C++ on Windows.

Installation:

  1. Download Visual Studio from visualstudio.microsoft.com
  2. Run the installer
  3. Select “Desktop development with C++” workload

Key Features:

  • IntelliSense code completion
  • Integrated debugger
  • CMake support
  • Profiling tools

Creating a New Project:

  1. File → New → Project
  2. Select “Console App” under C++
  3. Name your project and click Create

Project Structure:

MyProject/
├── MyProject.sln
└── MyProject/
├── MyProject.vcxproj
└── MyProject.cpp

JetBrains CLion is a powerful cross-platform C++ IDE.

Installation:

Key Features:

  • Smart code completion
  • Refactoring tools
  • CMake support
  • GDB/LLDB debugger integration

Xcode is Apple’s official development environment.

Installation:

  1. Open App Store
  2. Search for Xcode
  3. Click Install

Using Xcode for C++:

  1. Open Xcode
  2. File → New → Project
  3. Select “Command Line Tool”
  4. Choose C++ as the language

VS Code is a lightweight but powerful editor.

Installation:

  1. Download from code.visualstudio.com
  2. Install the C++ extension

Required Extensions:

  • C/C++ (Microsoft)
  • C++ Intellisense
  • CMake Tools (if using CMake)

Setting Up:

  1. Install a C++ compiler (see below)
  2. Create a tasks.json for building
  3. Create a launch.json for debugging

tasks.json Example:

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C++ Build",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}

Most Linux distributions come with GCC pre-installed. To verify:

Terminal window
g++ --version

If not installed:

Terminal window
# Ubuntu/Debian
sudo apt install g++
# Fedora/RHEL
sudo dnf install gcc-c++
# Arch Linux
sudo pacman -S gcc

macOS comes with Clang. Install Xcode Command Line Tools:

Terminal window
xcode-select --install

MinGW provides GCC on Windows:

  1. Download MinGW-w64 or use MSYS2
  2. Install and add to PATH

Or use Chocolatey:

Terminal window
choco install mingw

If you want to start immediately without installing anything:

Best for learning and experimenting:

  • Type code in browser
  • See assembly output
  • Multiple compiler versions

Cloud-based IDE:

  • Free tier available
  • Supports C++17
  • Collaborative features

Web-based compiler and debugger:

  • Compile and run in browser
  • Basic debugging features

Create a test program to verify everything works:

test.cpp
#include <iostream>
int main() {
std::cout << "C++ is working!" << std::endl;
// Print compiler info
#ifdef __GNUC__
std::cout << "Compiler: GCC " << __GNUC__ << "." << __GNUC_MINOR__ << std::endl;
#elif defined(_MSC_VER)
std::cout << "Compiler: MSVC " << _MSC_VER << std::endl;
#elif defined(__clang__)
std::cout << "Compiler: Clang " << __clang_major__ << "." << __clang_minor__ << std::endl;
#endif
return 0;
}

Compile and Run:

Terminal window
g++ -o test test.cpp
./test

Expected output:

C++ is working!
Compiler: GCC 11.3 (or your version)

As your projects grow, you’ll need build systems:

The classic Unix build tool:

# Makefile
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++17
TARGET = myprogram
SRC = main.cpp utils.cpp
$(TARGET): $(SRC)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRC)
clean:
rm -f $(TARGET)

Usage:

Terminal window
make
./myprogram
make clean

Cross-platform build system generator:

CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(myprogram main.cpp utils.cpp)

Build:

Terminal window
mkdir build
cd build
cmake ..
make
Build SystemBest For
Direct compileSingle files, learning
MakeSmall projects, Unix systems
CMakeCross-platform projects
NinjaLarge projects, faster builds
  • Beginner: Visual Studio Community
  • Professional: Visual Studio + ReSharper C++
  • Lightweight: VS Code + MinGW
  • Beginner: Xcode
  • Professional: CLion
  • Lightweight: VS Code + Homebrew GCC
  • Beginner: VS Code + GCC
  • Professional: CLion
  • Lightweight: Vim/Emacs + GCC

Essential settings:

Tools → Options → Text Editor → C++ → Formatting
Tools → Options → Projects → VC++ Directories

Create .vscode/settings.json:

{
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"editor.formatOnSave": true,
"files.associations": {
"*.h": "cpp"
}
}

Enable automatic CMake:

File → Settings → Build, Execution, Deployment → CMake

Solution: Install the compiler

  • Linux: sudo apt install g++
  • macOS: xcode-select --install
  • Windows: Install MinGW or Visual Studio

Solution: Install development headers

  • Linux: sudo apt install build-essential
  • Windows: Install Visual Studio with C++ workload

Solution: Run “Developer Command Prompt” from Visual Studio Start menu

Solution: Ensure compiler is in PATH, or specify manually:

Terminal window
cmake -DCMAKE_CXX_COMPILER=clang++ ..

C++ has two types of headers:

  1. C compatibility headers: <stdio.h>, <stdlib.h><cstdio>, <cstdlib>
  2. C++ headers: <iostream>, <vector>, <string>

Always prefer C++ headers:

// Good
#include <iostream>
#include <vector>
// Avoid (deprecated)
#include <iostream.h>

Let’s create a simple multi-file project structure:

MyFirstProject/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ └── utils.cpp
├── include/
│ └── utils.h
└── build/

include/utils.h:

#ifndef UTILS_H
#define UTILS_H
int add(int a, int b);
void greet(const std::string& name);
#endif

src/utils.cpp:

#include "utils.h"
int add(int a, int b) {
return a + b;
}
void greet(const std::string& name) {
std::cout << "Hello, " << name << "!" << std::endl;
}

src/main.cpp:

#include <iostream>
#include "utils.h"
int main() {
std::cout << "2 + 3 = " << add(2, 3) << std::endl;
greet("World");
return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(MyFirstProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include)
add_executable(myprogram
src/main.cpp
src/utils.cpp
)

Build and run:

Terminal window
mkdir build && cd build
cmake ..
make
./myprogram

Now that your environment is set up, let’s learn the basic syntax of C++ in the next chapter.

Next Chapter: 03_basic_syntax.md - Basic Syntax and Program Structure