2

Windows上使用VS code写C++代码

 2 years ago
source link: https://www.geekzl.com/windows-vscode-cpp.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Windows上使用VS code写C++代码

2021-02-12 12:43:53更新

要想进行debug,launch.json文件并不是必须的,只要有 tasks.json 文件即可。

最新操作:

0.安装MinGW, 将其bin目录加入到系统变量的PATH中; 在vs code中安装C/C++插件

1.打开你的 cpp 文件,按F1(或Shift + Ctrl + P)

2.输入C++, 选择"C/C++: Build and Debug Active File", 后面选"g++"

3.如果弹框提示错误, 点击Abort, 进行下面的4

4.切换到自动生成的 tasks.json 文件,删除或注释该文件中的 "type": "shell"一行

5.切换回你的 cpp 文件,重复步骤1和2, 就能正常进入debug状态了

觉得还不直观的小伙伴看下图:


如果在Windows上用过gdb编译过C++,一般都会对 MinGW有印象,Windows版的Eclipse for C++/Dev C++等都是基于MinGW来做的。于是我们在Windows上要想在VS code中用gdb的环境,同样需要先配置好MinGW的环境。

下面说说配置环境的主要步骤:

1.安装MS的C/C++插件

微软官方的 C/C++ 插件

https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

有需要还可以对该插件进行配置:

如果你安装了 Windows Terminal,还可以进行如下设置:

文件 -> 首选项 -> 设置 -> 用户 -> 拓展 -> Run Code Configuration

找到Run In Terminal勾选上

2.安装MinGW

下载地址: [Mingw-w64](https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/mingw-w64-install.exe/download)

运行安装包, 对于Architecture的选项,选择x86_64,然后下一步及后面的操作都按默认的来就好~

3.将gdb的bin目录添加到系统变量的PATH中

打开系统属性,在PATH变量中加入 bin 目录,比如 我的路径是 D:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin,加上去,关闭所有的cmd,重新打开cmd即可.

4.等安装完成, 测试是否work

gcc -v
g++ -v
gdb -v

5.配置build/Compile的环境

按快捷键 Ctrl + Shift + P 搜索 Task, 选择 Tasks: Configure Default Build Task

下一步 选择 g++

会看到一个默认的tasks.json文件,

由于已经为g设置好了系统变量中的PATH,于是command的完整路径"command": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"可以简写为 g++。

tasks.json文件

我自己用的最终版tasks.json为:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C/C++: g++.exe build active file",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

另外,需要删除这个默认 json 文件中的 "type": "shell", 接下来按快捷键ctrl+shift+B就可以build了。

6.配置debug环境

按F5,会弹出下面的框框:

选"C++ (Windows)"即可~

会弹出默认的 launch.json 文件, 将该内容替换为官方教程 https://code.visualstudio.com/docs/cpp/config-mingw 中的 launch.json文件.

launch.json文件官方教程版

{
  "version": "0.2.0",
  "configurations": [
   {
    "name": "g++.exe - Build and debug active file",
    "type": "cppdbg",
    "request": "launch",
    "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
    "setupCommands": [
     {
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
     }
    ],
    "preLaunchTask": "C/C++: g++.exe build active file"
   }
  ]
 }

由于已经为g++设置好了系统变量中的PATH,于是miDebuggerPath的完整路径可简化为 gdb。

于是综上,可得到精简版 launch.json:

launch.json文件精简版

我自己用的最终版launch.json为:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppdbg",  // default: cppvsdbg
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ],
    "preLaunchTask": "C/C++: g++.exe build active file"
}

另外还需要注意的是:

launch.json中的preLaunchTask需要与 tasks.json中的"label" 一致, 比如:

launch.json中的preLaunchTask值为"compile"

而tasks.json中Label的值也需要为"compile":

7.开始debug

接下来在代码中加上断点,就可以按F5愉快地进行debug了。

附录: tasks.json中参数的解释

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [{
        "label": "compile", // 任务名称,与launch.json的preLaunchTask相对应
        "command": "g++",   // 要使用的编译器,C++用g++
        "args": [
            "${file}",
            "-o",    // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 生成和调试有关的信息
            "-m64",  // 不知为何有时会生成16位程序而无法运行,此条可强制生成64位的
            "-Wall", // 开启额外警告
            "-static-libgcc",     // 静态链接libgcc,一般都会加上
            "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
            "-D__USE_MINGW_ANSI_STDIO", // 用MinGW写C时留着,否则不需要,用于支持printf的%zd和%Lf等
        ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
        "type": "process", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
        "group": {
            "kind": "build",
            "isDefault": true // 不为true时ctrl shift B就要手动选择了
        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档,即使设为never,手动点进去还是可以看到
            "focus": false,     // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
            "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
        },
        "problemMatcher":"$gcc" // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
        // 本来有Lint,再开problemMatcher就有双重报错,但MinGW的Lint效果实在太差了;用Clangd可以注释掉
    }]
}

来源:
https://code.visualstudio.com/docs/cpp/config-mingw

5 / 5 ( 2

votes )


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK