Setting Up a Programming Environment in Windows: VSCode with CygWin g++
Steven J. Zeil
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:
- Fairly easy setup.
- No need to run an X server package.
- You can do most of your work in Windows, but still have the option of stepping into a CygWin
bash
window for command-line operations.
Disadvantages of this approach:
- Cygwin uses Unix-style paths. For example, if you are working in a project directory with source code contained in a
cpp
directory within an outersrc
directory, in Windows you would access this source code assrc\cpp\
, but in Cygwin this would besrc/cpp
.Absolute paths are trickier. The Windows directory
C:\Users\yourName\projects
would be, in Cygwin,/cygdrive/c/Users/yourName/projects
.- This can cause some confusion when Windows programs (e.g.,
Eclipse
) need to talk to Cygwin programs (e.g.,g++
orgdb
).
- This can cause some confusion when Windows programs (e.g.,
1 Installing the CygWin Compiler
-
Go to the CygWin site and follow the directions to download and then run the CygWin installer program.
-
I recommend just installing the basic, default CygWin system on your first “pass” without adding any optional packages (including the compiler).
-
Check your installation to make sure it works. You should have a new
Start
menu entry to run aCygWin terminal
. Try it and make sure that it works. It should run abash
command shell that accepts commands like:pwd
: to print your current working directory locationls
: to list the files and directories in your current working directorycd
directory : to change your current working directory locationexit
: to close the terminal session.
-
If everything looks good, re-run the CygWin setup program and add the following:
g++
,gdb
,gcc-debuginfo
,git
, andmake
.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
. -
Check your installation to make sure it works. Start a
CygWin terminal
again. Give the commandsg++ --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.
-
Get VSCode here and install it on your PC.
-
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.
-
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
-
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
-
-
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.
- 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”.
-
If your project has a make file:
In the new
tasks.json
file,- Change the
label
to “C/C++ make -k” - Change the
command
to “make” - Change the
args
to [“-k”] - Change the
cwd
option to “${workspaceFolder}”
You should wind up with something like this:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: make -k", "command": "make", "args": [ "-k" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "compiler: C:\\cygwin64\\bin\\cpp.exe" } ] }
- Change the
-
If your project does not have a makefile,
In the new
tasks.json
file,- Change the
label
to “C/C++ build all” - In the
args
area, change “${file}” to "*.cpp". - Change the
cwd
option to “${workspaceFolder}”
You should wind up with something like this:
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build all", "command": "C:\\cygwin64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "*.cpp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "compiler: C:\\cygwin64\\bin\\g++.exe" } ] }
- Change the
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,
- Change the
name
to “(gdb) CygWin launch”. - For the program, put “${workspaceFolder}/???.exe”, replacing the ??? by hte name of your executable program.
- If your program needs CLI arguments when it is run, put hose into
args
as a comma-separated sequence of quoted strings. - For
cwd
, put “${workspaceFolder}”. - For
miDebuggerPath
, put “gdb”. - 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
}
]
}
]
}