vscode+cpp+mingw64环境配置

参考:https://code.visualstudio.com/docs/cpp/config-mingw

前提条件:

进行环境配置之前请先做好以下工作

1、安装vscode

2、安装 c++ extension for VS Code (https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

3、安装mingw-w64。注意安装路径不能有空格。比如可以安装到 d:Mingw-w64

4、将Minge-w64的bin文件夹路径添加到windows的PATH环境变量里。比如我的路径是 c:mingw-w64x86_64-8.1.0-win32-seh-rt_v6-rev0mingw64in

在vscode中创建工作区

方式1:点击菜单“文件”->“打开文件夹”,选择一个文件夹(比如project1),则文件夹project1就变成当前工作区。

方式2:

在命令行执行以下命令

mkdir helloworld
cd helloworld
code .

hellowork目录就成了当前工作区。  

配置完成后会在工作区文件夹生成下面的文件

  • c_cpp_properties.json (compiler path and IntelliSense settings)(编译器路径和智能感知设置)
  • tasks.json (build instructions)(生成说明)
  • launch.json (debugger settings)(调试设置)

设置编译器路径

按 Ctrl+Shift+P,打开命令面板

在弹出的窗口中输入 C/C++,在弹出的列表中选择“Edit Configurations (UI)”,然后系统会打开C/C++ IntelliSense Configurations 页面。在此页面所做的修改会被保存到文件c_cpp_properties.json

将编译器路径修改为上面安装的mingw-w64的路径(D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe)

将IntelliSense 模式修改为 gcc-x64

我配置出来的文件是这样的

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": [],
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true
            }
        }
        
    ],
    "version": 4
}
.vscode/c_cpp_properties.json

创建生成任务

本小节创建一个tasks.json文件,告诉vscode如何生成程序。生成任务会调用g++程序将源码编译成可执行文件。

1、按下 Ctrl+Shift+P 打开命令面板--》输入 "task"--》选择 Tasks: Configure Default Build Task.--》在列表中选择 从模板创建 tasks.json文件,--》选择 Others. VS Code 会创建一个最小的tasks.json 文件并且打开。

用下面的内容替代文件的内容

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "shell",
        "command": "g++",
        "args": [ "-g",
                            "${file}",
                           "-o",
                           "${fileDirname}\${fileBasenameNoExtension}.exe"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }
tasks.json

配置调试设置

本小节的设置是为了当用户按下F5时让VSCode启动gcc调试器

在命令面板输入 "launch" ,软后选择 Debug: Open launch.json. 然后选择 the GDB/LLDB,然后选择“g++.exe build and debug active file”.

生成后的文件如下

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "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": "D:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}
launch.json

 

配置完毕

原文地址:https://www.cnblogs.com/niao-ge/p/12023962.html