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

六月ゝ 毕业季﹏ 提交于 2019-12-11 04:56:28

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/56457209/setting-up-vs-code-for-c-using-cygwin64-compiler-and-debugger-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!