Setting Up a Programming Environment in Windows: VSCode with CygWin g++

Steven J. Zeil

Last modified: Nov 19, 2023
Contents:

Since the mid 1990’s, Cygwin has provided a powerful environment for porting Unix programs into the Windows world. Technically, CygWin provides a “POSIX” layer (POSIX being a standard for providing compatibility between operating systems). From a practical point of view, Cygwin allows a Windows PC to run the bash shell with most of the common Unix commands.

This document will walk you through setting up a Windows/Cygwin programming environment using: * the VSCode IDE running natively on Windows, * Java running natively on Windows, and * g++, make and gdb in CygWin.

Advantages of this approach:

Disadvantages of this approach:

1 Installing the CygWin Compiler

  1. Go to the CygWin site and follow the directions to download and then run the CygWin installer program.

  2. I recommend just installing the basic, default CygWin system on your first “pass” without adding any optional packages (including the compiler).

  3. Check your installation to make sure it works. You should have a new Start menu entry to run a CygWin terminal. Try it and make sure that it works. It should run a bash command shell that accepts commands like:

    • pwd: to print your current working directory location
    • ls: to list the files and directories in your current working directory
    • cd directory : to change your current working directory location
    • exit: to close the terminal session.
  4. If everything looks good, re-run the CygWin setup program and add the following: g++, gdb, gcc-debuginfo, git, and make.

    I recommend always getting the highest-numbered version that is not marked as “Test”.

    However, if you have problems getting the gdb debugger to work, try using version 9.2.1 of gdb.

  5. Check your installation to make sure it works. Start a CygWin terminal again. Give the commands

    g++ --version
    gdb --version
    make --version
    

    Each should result in a message indicating that the program started successfully.

2 Install VSCode

Next up is the VSCode IDE.

  1. Get VSCode here and install it on your PC.

  2. Run VSCode. In a freshly installed state, it does not know how to work with C++ or Java, and it does not know how to do remote development. We’ll fix these limitations by immediately installing some extensions.

    Click on the button to enter the list of extensions.

  3. Use the text box at the top of the left column to search for “C++”. Locate and install the following extensions:

    • C/C++
    • Better C/C++ Syntax
  4. If you are interested in Java programming, use the text box at the top of the left column to search for “Java”. Locate and install the following extensions:

    • Java Extension Pack

  5. Exit VSCode.

3 Add the Compilers to your PATH

VSCode needs to be able to find your newly-installed compiler. There is a chance that it will find them if you have simply installed them in their default locations, but I find that things run more smoothly if the compiler and related tools are in your PATH.

The %PATH% in Windows is the list of directories where Windows will search for executable programs.

There are two ways to do this:

3.1 The “Permanent” Way

1.Add the bin directory containing the g++ and other Cygwin executables (C:\cygwin64\bin, if you have accepted the default location when installing Cygwin), to your Windows PATH.

  1. Check that you have done so successfully: Open a Windows cmd window. (Click the Windows start button and type “cmd”).

    Enter the following commands:

    g++ --version
    

    gdb –version make –version

    Each should result in a message indicating that the program was located and started successfully.

3.2 The Temporary Way

Create a Windows batch file to launch Eclipse, e.g., cygEclipse.bat that adds the bin directory containing the g++ and other Cygwin executables (C:\cygwin64\bin, if you have accepted the default location when installing Cygwin), to the Windows PATH and then launches Eclipse:

cygVSCode.bat

SET PATH=%PATH%;c:\cygwin64\bin
"C:\Users\jdoe\AppData\Local\Programs\Microsoft VS Code\Code.exe"

Change this path to the actual path where your Cygwin bin file is stored, and change this path to the place where you installed Eclipse.

If you opt for this approach rather than the “permanent” change to %PATH%, then you will need to remember to use this batch file to launch VSCode whenever you want to work in C++.

I like to put this .bat file right on my desktop so that I can easily launch it by double-clicking on it.

4 Try it Out

You should now be able to create C++ projects, edit C++ code, compile it, and debug it.

Try the VSCode exercises from here and here in CS252, skipping the step of connecting to a remote server.

To configure your build and launch tasks:

4.1 Build task

Select a .cpp file. Then from the Terminal menu, select “Configure Default Build Task”.
Select “C/C++: g++.exe build active file”.

4.2 Launch Task

To configure your launch task (for launching the debugger), select any .cpp file. Then, in the Run menu, select “Add Configuration”. Select “C++ (gdb/lldb)”. Then select the same menu entry again and choose “C/C++ (gdb) Launch”.

In the resulting launch.json file,

  1. Change the name to “(gdb) CygWin launch”.
  2. For the program, put “${workspaceFolder}/???.exe”, replacing the ??? by hte name of your executable program.
  3. If your program needs CLI arguments when it is run, put hose into args as a comma-separated sequence of quoted strings.
  4. For cwd, put “${workspaceFolder}”.
  5. For miDebuggerPath, put “gdb”.
  6. Optional, but recommended: Add an entry after the miDebuggerPath:

    "preLaunchTask": "name-of-your-build-task",

You should wind up with something like this:

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "(gdb) Cygwin Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/testcpp.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "preLaunchTask": "C/C++: make -k",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}