在 Windows 上使用 Cygwin64 编译器和调试器为 C 设置 VS Code

api*_*ous 6 c cygwin visual-studio-code vscode-tasks

想要设置 VS Code 以与 Cygwin/Cygwin64 一起使用。已经设置了这些:

  1. 在windows上安装了Cygwin64
  2. 从 Cygwin 安装程序安装 gcc(编译器)和 gdb(调试器)包
  3. GCC 和 GDB 不在Windows路径中。
  4. 安装的 Visual Studio 代码

发布此内容是因为我花了几天时间从多个不同的来源进行设置。这是专门针对安装了 Cygwin/Cygwin64 的 Windows 的。

免责声明:我仅针对构建单个文件进行了测试。

api*_*ous 8

这里的说明是关于 VS Code 上的设置

  1. 在 VS Code 上安装扩展 C/C++
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
Run Code Online (Sandbox Code Playgroud)
  1. 如果您已有工作区,请跳过此步骤。

    创建一个文件夹并将该文件夹添加到 VS Code 中。然后保存工作区。

  2. 设置launch.json

    转到“调试 > 打开配置”,这应该打开 launch.json 文件。以下是我的配置。如果您正在对此进行测试并且不确定自己在做什么,我建议您在替换内容之前将原始内容保存在某处。

    注意:"preLaunchTask": "gcc.exe build active file"运行标记为“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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)
  1. 设置task.json

    转到“终端 > 配置任务...”并选择“gcc.exe 构建活动文件”

    “args”中的各种“-W”标志旨在使编译器更加严格。如果您愿意,可以将其删除。

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)
  1. 构建和调试活动文件

    转到要构建的 C 文件,按 Ctrl+Shift+P 进入“命令面板 > C/C++ 构建和调试活动文件 > gcc.exe 构建活动文件”,或者如果您只想构建,则转到“终端 >运行构建任务”。