Guide To Set Up C/C++ Dev Environment

Guide To Set Up C/C++ Dev Environment

How To Read This Guide

This guide is meant for beginners as well as readers with basic knowledge. Most of the technical terms have been explained or have been linked to a webpage with more information.

If you still don't understand a term, just Google it :)

This guide explains how to install as well as use a C/C++ compiler to run your code. The compiler used here is GCC.

To write and edit code, any text editor such as Notepad can be used, although, a code editor is recommended because of syntax highlighting, code completion, and much more. Here, we will be setting up a popular cross-platform code editor - Visual Studio Code.

This guide has separate sections for different operating systems. Use the Table of contents to jump to the relevant section corresponding to the operating system of your interest.

NOTE: You are supposed to enter and run a command in a terminal whenever you are given one, in this blog.

A Terminal generally refers to:


Windows

Here, the native Windows version of GCC - called MinGW - will be installed.

There are quite a few methods to achieve this. We will be looking into two of them - one being the most widely popular; using the MinGW installer directly from its website - other being the simplest; using the Chocolatey package manager.

Use the Table of contents to jump to the method of your choice.

Using MinGW installer

  1. Download the installer file mingw-get-setup.exe from https://osdn.net/projects/mingw/releases/.

    image

  2. Run the .exe file. A window titled "MinGW Installation Manager" will open in a few minutes. In the Basic Setup tab, right-click the option named mingw32-gcc-g++-bin and click on Mark for Install.

    image

  3. Then in the top left corner click on Installation > Apply Changes. Continue through the rest of the installation process while keeping all settings as default. And then, you wait...

    image

GCC is installed! Now we have to make it accessible by adding it to the PATH:

  1. Go to Windows Explorer > Right-click on "This PC" > Properties > Advanced system settings > Environment Variables.

    image

  2. At the bottom System Variables panel, look for a Variable named PATH and double-click on it.

  3. Click the New button to add a new path to Windows PATH.

  4. Enter the Path of the folder to which you installed the GCC (by default, it's C:\MinGW\bin)

    image

  5. Press OK until you are out of the menus.

You should now have the GCC installed and ready to be used! To verify, type gcc --version in Command Prompt and press Enter; if you get an output stating its version, then it was successfully installed.

image

The easier way (via chocolatey)

Chocolatey is a package manager for windows that makes installing applications a lot easier and more automatable. As opposed to the long process we went through in the above way, via chocolatey the entire process of installing chocolatey can be reduced to just 3 commands - executed in an Administrator Powershell:

  1. Install chocolatey with the instructions from their website: https://chocolatey.org/install

    • Run Get-ExecutionPolicy in an administrator PowerShell. If it returns Restricted, then run Set-ExecutionPolicy AllSigned.

    • Now run:

        Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
      

      and wait for the installation of chocolatey to be complete.

  2. Close and reopen the admin PowerShell session.

  3. Run choco install mingw and type A to confirm the installation.

    image

    You should see this if it succeeds

    image

  4. Now run gcc --version to verify that it has been installed properly.

    image


Linux

Most Linux distributions will have GCC pre-installed. To check if it is installed:

gcc --version

If you get some output; NOT an error, then it is installed.

On Ubuntu and other Debian-based distributions: Run the following commands in the Terminal:

sudo apt update
sudo apt install build-essential

For other distributions just install the package build-essential from your package manager.


MacOS

Though GCC "appears" to be installed in MacOS by default, it's clang in disguise!

image

Why bother?

For the audience one of the main drawbacks of using clang can be not able to use <bits/stdc++. h>, for your work. To know about the differences in-depth - refer to:

Getting GCC installed on MacOS

  1. Install homebrew - ignore if already present.

    • To check whether it is installed or not use the following command

        command -v brew
      
    • If the output is like the following then brew is installed!

        ~> command -v brew
        /opt/homebrew/bin/brew
      
  2. Installing GCC.

     brew update
     brew upgrade
     brew install gcc
    

    image

    Note
    How GCC is installed as gcc-12 and gcc is still clang.

  3. Now, you can either use gcc-12 instead of gcc. But if you want gcc to invoke the GNU version of GCC instead of clang then execute the following lines in your terminal. Since MacOS comes with zsh by default we will be giving command for it only but if you have changed your shell then you must be equipped enough to do the following steps on that shell too - Right?

     echo "alias gcc=/opt/homebrew/bin/gcc-12" >> ~/.zshrc
    

    image

Unprecendeted issue with MacOS 13(Ventura) & gcc-12

image

If you are using MacOS Ventura and facing this issue - which means it's not fixed yet - no problem, then read the following issue/discussion to solve it!

Yes. You are going to read all of that, try to understand; try to fix. Repeat until you succeed! You have to start self-learning sometime later, so why not start now?


Compiling the code manually

Follow these steps to compile your .c file manually.

  1. Create a file with the name first_c_program.c with the following content in it and then save it.

     #include <stdio.h>
    
     int main(){
         printf("Hello World!");
    
         return 0;
     }
    

    ⚠️ Make sure you don't have spaces in your filename!

  2. Compile the program and execute it. You can replace executable.exe with anything.anything.

    image

    For Windows users, the command to run will be the following, the above works for Linux & MacOS only.

     .\filename.exe
    

Setting Up VS Code

Visual Studio Code is a free and open-source code editor that is customizable through extensions. The C/C++ extension can be used for compiling and running C/C++ code along with many more features.

Installing

  1. Open VS Code

  2. Open the extensions panel using the button on the left or by pressing Ctrl+Shift+X.

  3. Search and install the C/C++ extension.

    image

Once the extension is installed, creating or opening any .c or .cpp file will have syntax highlighting, code completion, and other features.

image

Compiling

To compile the code, open a file and run Terminal > Build Task from the main menu or press Ctrl + Shift + B.

image

On Windows: From the dropdown, select C/C++: g++.exe build active file(for C++ files) or C/C++: gcc.exe build active file(for C files).

On Linux and MacOS: From the dropdown, select C/C++: g++ build active file(for C++ files) or C/C++: gcc build active file(for C files).

image

Running

Once the code has been compiled, an executable file will be created (with the same name as the code file). On Windows, this file will have a .exe extension on Windows and no extension on macOS and Linux.

To run the file:

  1. Open the VS Code integrated terminal by pressing Ctrl + `.

  2. Type:

    • On Windows: .\ followed by the name of the executable file.

    • On Linux and macOS: ./ followed by the name of the executable file.

and press enter.

image

  1. The output of the code will be visible in the terminal!

Code Runner

Depending on your OS, we suggest different code runners. Hop into the section of your interest using the Table of Contents.

Windows 11

You are suggested to use code-runner using WSL instead of using this buggy .run extension which will be installed in this section ahead - which is shared just as an alternative that is relatively easy to set up.

  1. Click on the extension's icon.

  2. Search for code runner.

  3. Install the extension shown in the screenshot below.

    image

Linux and MacOS

For simplicity and to automate a few steps for a list of languages you can try code-runner.

image

Installation

Copy and paste the following command in your terminal and wait for the final message - you are done!

curl https://raw.githubusercontent.com/proffapt/code-runner/main/setup.sh | /bin/bash

Can help in the following ways:

  • Just one command to compile and execute the compiled file: run filename.extension.

  • Cleans compiled binary by default. So, that you don't submit binary instead of a .c file in your assignments.

  • Detects the version of python to be used ( accuracy of 95% ).

  • Can be used to create a file with spaces in its name ( vs-code runner extension lacks this )

  • Can be used to execute a file in a different directory ( vs-code runner extension lacks this; and limits some functionalities )

  • Inbuilt debugger support.

  • Integrates with NeoVIM and VS-CODE - steps in the documentation of the tool.

And many more, read the documentation and code for a thorough understanding at https://github.com/proffapt/code-runner.


Tips and Further Reading

DO NOT GUESS how a piece of software might work.

When using a tool for the first time, you CAN'T and MUSTN'T guess how it works! Beginners often make this mistake and waste a lot of their time and of others too; without even searching or reading the docs!
Rather read the documentation/help for that program or just simply search for whatever you wanna achieve with that tool.

Read The Fucking Manual - RTFM

This is a starting guide, so it was a bit explanatory. What we promote is self-learning. The basic idea behind it is very simple:

  1. Read the error carefully and try to understand it.

  2. Phrase your problem/goal.

  3. Search it on your favourite search engine before asking in groups.

  4. Either you will get the solution, if not then ask in groups/forums.


Other Authors

Did you find this article valuable?

Support Arpit Bhardwaj by becoming a sponsor. Any amount is appreciated!