在Windows上使用Cygwin64编译器和调试器为C设置VS代码 [英] Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows

查看:327
本文介绍了在Windows上使用Cygwin64编译器和调试器为C设置VS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要设置VS Code以与Cygwin/Cygwin64一起使用. 已经进行了以下设置:

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

  1. 在Windows上安装了Cygwin64
  2. 从Cygwin安装程序中安装了gcc(编译器)和gdb(调试器)软件包
  3. GCC和GDB在Windows路径中.
  4. 已安装的Visual Studio代码
  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

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

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.

推荐答案

此处的说明用于在VS Code上进行设置

Instructions here are for setting up on 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

  1. 如果已经有工作区,请跳过此步骤.

  1. If you have a workspace already, skip this step.

创建一个文件夹,然后将此文件夹添加到VS Code中.然后保存工作区.

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

设置launch.json

转到调试>打开配置",这将打开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.

注意:"preLaunchTask": "gcc.exe build active file"运行标有"gcc.exe构建活动文件"的任务.

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. 设置task.json

转到终端>配置任务...",然后选择"gcc.exe构建活动文件"

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

"args"中的各种"-W"标志旨在使编译器更加严格.您可以根据需要删除它们.

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. 构建和调试活动文件

  1. Build and Debug Active File

转到要构建的C文件,按Ctrl + Shift + P的命令面板> C/C ++生成和调试活动文件> gcc.exe生成活动文件",或者如果仅要生成,请转到终端>运行构建任务".

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

这篇关于在Windows上使用Cygwin64编译器和调试器为C设置VS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆