Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows

12,177

Instructions here are for setting up on VS Code

  1. Install the extension C/C++ on VS Code
Name: C/C++
Id: ms-vscode.cpptools
Description: C/C++ IntelliSense, debugging, and code browsing.
Version: 0.23.1
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
  1. If you have a workspace already, skip this step.

    Create a folder and add this folder into VS Code. Then save workspace.

  2. Set up launch.json

    Go to "Debug > Open Configurations", this should open the launch.json file. Below is my configuration. If you're testing this and unsure of what you're doing, I suggest you save your original content somewhere before replacing things.

    Note: "preLaunchTask": "gcc.exe build active file" runs the task labelled "gcc.exe build active file".

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "%PATH%;z:\\cygwin64\\bin"
                }
            ],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "logging": { "engineLogging": true }, //optional
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}
  1. Set up task.json

    Go to "Terminal > Configure Tasks..." and select "gcc.exe build active file"

    The various "-W" flags in "args" are meant to make the compiler more strict. You can remove them if you'd like.

{
    "tasks": [
        {
            "type": "shell",
            "label": "gcc.exe build active file",
            "command": "C:\\cygwin64\\bin\\gcc.exe",
            "args": [
                "-g",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-Werror", // Optional
                "-Wall", // Optional
                "-Wextra", // Optional
                "-ansi", // Optional
                "-pedantic", // Optional
                "${file}"
            ],
            "options": {
                "cwd": "C:\\cygwin64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ],
    "version": "2.0.0"
}
  1. Build and Debug Active File

    Go to the C file you want to build, press Ctrl+Shift+P for "Command Palette > C/C++ Build and Debug Active File > gcc.exe build active file" or if you only want to build then go to "Terminal > Run Build Task".

Share:
12,177
stephw
Author by

stephw

Updated on June 04, 2022

Comments

  • stephw
    stephw almost 2 years

    Would like to set up VS Code to work with Cygwin/Cygwin64. Already have these set up:

    1. Installed Cygwin64 on windows
    2. Installed gcc (Compiler) and gdb (Debugger) packages from Cygwin installer
    3. GCC and GDB are NOT in windows path.
    4. Installed Visual Studio Code

    Posting this because it took me a couple of days from multiple different sources to set this up. This is specifically for Windows with Cygwin/Cygwin64 installed.

    DISCLAIMER: I've only tested this for building single files.