cyberangles blog

Setting up a C++ Development Environment

C++ is a powerful and widely used programming language. To start developing C++ applications, you need to set up a proper development environment. In this blog post, we will guide you through the process of setting up a C++ development environment on different operating systems (Windows, macOS, and Linux). We will also cover some common practices and best practices along the way.

2026-07

Table of Content#

  1. Choosing a Text Editor or IDE
  2. Installing a Compiler
    • GCC (GNU Compiler Collection)
    • Clang
    • Microsoft Visual C++ Compiler (Windows Only)
  3. Setting up the Environment Variables (Windows and Linux)
  4. Writing and Compiling Your First C++ Program
  5. Common Practices and Best Practices
    • Using Version Control (e.g., Git)
    • Code Formatting and Style Guides
    • Testing Your Code
  6. Example Usage
  7. References

1. Choosing a Text Editor or IDE#

Text Editors#

  • Visual Studio Code: A lightweight, cross - platform text editor with excellent C++ support. It has a vast ecosystem of extensions for features like code completion, debugging, and more.
  • Sublime Text: Known for its speed and simplicity. It can be customized with plugins to enhance C++ development.

Integrated Development Environments (IDEs)#

  • Microsoft Visual Studio (Windows): A full - fledged IDE with powerful debugging, code analysis, and project management features for C++ development on Windows.
  • Xcode (macOS): The official IDE for macOS and iOS development. It has good support for C++ projects.
  • CLion (Cross - Platform): Developed by JetBrains, it offers a rich set of features like intelligent code completion, refactoring, and debugging for C++ development.

2. Installing a Compiler#

GCC (GNU Compiler Collection)#

  • Linux:
    • Most Linux distributions come with GCC pre - installed. You can check the version by running gcc --version in the terminal.
    • If not installed, on Debian - based systems (e.g., Ubuntu), you can install it using sudo apt-get install build-essential. This will install GCC along with other build tools.
  • macOS:
    • You can install GCC using Homebrew. First, install Homebrew if not already installed. Then run brew install gcc.
  • Windows:
    • You can use MinGW (Minimalist GNU for Windows). Download the MinGW installer from the official website. During the installation, select the gcc package.

Clang#

  • Linux and macOS:
    • Clang is often included in the system's package manager. On Ubuntu, you can install it with sudo apt-get install clang. On macOS with Homebrew, run brew install llvm (Clang is part of the LLVM project).
  • Windows:
    • You can download the pre - built binaries from the LLVM website and set up the environment variables (similar to GCC on Windows).

Microsoft Visual C++ Compiler (Windows Only)#

  • If you are using Microsoft Visual Studio, the Visual C++ compiler is included. You can also install the Build Tools for Visual Studio separately from the Visual Studio installer.

3. Setting up the Environment Variables (Windows and Linux)#

Windows#

  • For MinGW (GCC on Windows):
    • After installing MinGW, add the bin directory (e.g., C:\MinGW\bin) to the PATH environment variable.
    • Right - click on This PC (or My Computer), go to Properties -> Advanced system settings -> Environment Variables.
    • In the System Variables section, find the PATH variable, click Edit, and add the MinGW bin directory path.

Linux#

  • For GCC or Clang:
    • The compiler's binary paths are usually already in the PATH if installed via the package manager. But if you have installed from source or in a non - standard location, you may need to add the path to the PATH variable.
    • You can edit the ~/.bashrc (for Bash shell) or the appropriate shell configuration file. For example, if your compiler is in /usr/local/bin, add the line export PATH=$PATH:/usr/local/bin to the shell configuration file and then run source ~/.bashrc to apply the changes.

4. Writing and Compiling Your First C++ Program#

Writing the Code#

Open your chosen text editor or IDE and create a new file with a .cpp extension (e.g., hello.cpp).

#include <iostream>
 
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Compiling the Code#

  • Using GCC:
    • In the terminal (navigate to the directory containing hello.cpp), run g++ hello.cpp -o hello (the -o option is used to specify the output executable name).
  • Using Clang:
    • Run clang++ hello.cpp -o hello in the terminal.
  • Using Microsoft Visual C++ Compiler (in Visual Studio):
    • Create a new Console Application project. Add the hello.cpp code to the main.cpp (or equivalent) file. Then build the project (usually by pressing F7 or using the Build menu).

5. Common Practices and Best Practices#

Using Version Control (e.g., Git)#

  • Initialize a Git repository in your project directory using git init.
  • Commit your code regularly. For example, git add. (add all changes) and git commit -m "Initial commit of hello world program".
  • You can also push your code to a remote repository (e.g., GitHub, GitLab) for backup and collaboration.

Code Formatting and Style Guides#

  • Follow a C++ style guide like the Google C++ Style Guide or the C++ Core Guidelines.
  • Many IDEs and text editors have plugins or built - in features to format code according to a style guide. For example, Visual Studio Code has the Format Document command (usually Ctrl+Shift+I or Cmd+Shift+I on macOS) which can be configured to use a specific style.

Testing Your Code#

  • Write unit tests for your functions. You can use testing frameworks like Google Test.
  • For example, to test a simple function:
// mymath.cpp
int add(int a, int b) {
    return a + b;
}
 
// test_mymath.cpp
#include <gtest/gtest.h>
#include "mymath.cpp"
 
TEST(AddTest, PositiveNumbers) {
    EXPECT_EQ(add(2, 3), 5);
}

Compile and run the tests using the appropriate build commands for your compiler and testing framework.

6. Example Usage#

Let's say you are developing a simple calculator program.

Code#

#include <iostream>
 
int add(int a, int b) {
    return a + b;
}
 
int subtract(int a, int b) {
    return a - b;
}
 
int multiply(int a, int b) {
    return a * b;
}
 
int divide(int a, int b) {
    if (b == 0) {
        std::cerr << "Error: Division by zero" << std::endl;
        return 0;
    }
    return a / b;
}
 
int main() {
    int num1, num2;
    char operation;
 
    std::cout << "Enter first number: ";
    std::cin >> num1;
    std::cout << "Enter operation (+, -, *, /): ";
    std::cin >> operation;
    std::cout << "Enter second number: ";
    std::cin >> num2;
 
    int result;
    switch (operation) {
        case '+':
            result = add(num1, num2);
            break;
        case '-':
            result = subtract(num1, num2);
            break;
        case '*':
            result = multiply(num1, num2);
            break;
        case '/':
            result = divide(num1, num2);
            break;
        default:
            std::cerr << "Invalid operation" << std::endl;
            return 1;
    }
 
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Compilation#

Using GCC: g++ calculator.cpp -o calculator

7. References#