Installing C/C++ Programming Environment

programming Aug 02, 2020

This post illustrates how to set up the Programming Environment for C and C++ languages. To set up our environment, we shall use the GNU Compiler Collection (GCC) and Visual Studio Code as the editor on a Windows 10 device.

GCC Installation

We first need to install the GCC compiler on windows. Since GCC was originally created for GNU operating system, to install it on windows, we need to install Minimalist GNU for Windows (MinGW). The file mingw-get-setup.exe can be downloaded from here.

Follow the on-screen instructions to install MinGW-Get setup. Post completion of installation, on the last screen, press "Quit" instead of "Continue" to exit the setup.

Open a Command Prompt window and type the following to install GCC through MinGW:
mingw-get install gcc g++ gdb

The above command will install the c compiler, c++ compiler and the GNU debugger along with their dependencies, headers and libraries.

In case the command prompt throws a command not recognized error (as below):
'mingw-get' is not recognized as an internal or external command, operable program or batch file.

It means that we need to set the environment path variable for mingw-get or directly run it from source directory. Typically the source directory for mingw-get is: C:\MinGW\bin

Once the installation of gcc, g++, and gdb is finished, we can verify the same through command prompt as following:

  • For GDB:
    >gdb GNU gdb (GDB) 7.6.1 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "mingw32". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. (gdb)
  • For G++:
    >g++ g++: fatal error: no input files compilation terminated.
  • And for GCC:
    >gcc gcc: fatal error: no input files compilation terminated.

Getting the above outputs mean that the compilers and debugger for C and C++ have been successfully installed.

Visual Studio Code Installation

Visual Studio Code can be installed from here. Follow the on-screen installation instructions for VS Code. Post installation, we need to add the C/C++ extensions.

  1. Launch VS Code and at the left toggle, click the button for Extensions.
  2. Search for Extensions for C.
  3. Install the Extensions C/C++ by Microsoft and C/C++ Compile Run by danielpinto8zz6

2020-08-02_165802

Debugger Configuration

We now need to configure the VS code debugger to use gdb for debugging C/C++ files. To do so, we must first write simple program. Here we are writing a Hello World program. Press Ctrl+N to create a new file and add the following:

#include <stdio.h>

int main(void)
{
    printf("Hello World\n");
}

Save this program in folder. This folder will contain your future code and output files. Now do the following.

  1. At the left toggle, click on the Run Button.
  2. This will open the run panel. Click on the orange "Run and Debug" button.
  3. In the option choose C++ (GDB/LLDB)
  4. On the next option choose gcc.exe
  5. This will open the launch.json file
  6. Comment out the existing path for miDebuggerPath and replace it with the path of GDB file:
    "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",

2020-08-02_172417

That's All Folks!

This should set up your C/C++ programming environment. You can use F5 for debugging and F6 for compiling your code. Visual Studio Code's complete Keyboard Cheatsheet can be found here.

Sreyash Ratna Tripathi

I'm a Cyber Security Professional, Graduate Student at Carnegie Mellon University, and Co-creator of REScure Threat Intelligence Platform.