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

2

6

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.

Tombouctou answered 5/6, 2019 at 8:54 Comment(0)
T
8

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".

Tombouctou answered 5/6, 2019 at 8:54 Comment(1)
launch.js and task.js must be in the .vscode folder which is in the folder where the .cpp file is storedSiderite
D
-3

Cygwin64 is inefficient because it is using a medium memory model. This makes it use 64 bit absolute addresses instead of 32 bit relative addresses for static data.

I will recommend to use the Clang plugin for Visual Studio instead, unless you have a specific reason to use Cygwin64. See https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/

You also get rid of the cygwin DLL when using the Clang plugin.

Dud answered 28/9, 2019 at 7:48 Comment(3)
Is this wrt VSCode similar to the original question, or Visual Studio?Haemolysin
I think clang works fine with both Visual Studio and VSCode.Dud
The original question was about Cygwin integration, so answers of the form "don't use Cygwin" seem off-topic.Oops

© 2022 - 2024 — McMap. All rights reserved.