VsCode-C/C++编译环境配置

前期准备

下载 VsCode

官网地址:Code Editing

下载 MinGW64编译器

官网地址:MinGW64

配置系统环境变量

Win+I 打开系统设置
找到 编辑系统环境变量
点击 高级 > 环境变量 > 选定 Path 进行编辑
添加进你安装的 MinGW64 编译器的 路径
例如我的就是:D:\Program\mingw-w64\mingw64\bin

配置相关文件

安装 C/C++ 扩展

进入 VsCode 找到左侧栏的 Extensions
一般安装如下扩展:

  • C/C++
  • Chinese (Simplified)(简体中文)
  • Code Runner

设置编译器路径

新建 Code 文件夹用来保存项目文件
在 Code 文件夹中右键点击 用Code打开 进入 VsCode
接下来配置编译器路径
按快捷键Ctrl+Shift+P调出命令面板,输入C/C++,选 Edit Configurations(UI) 进入配置

在里面找到 编译器路径 修改成 MinGW64路径,再将下方的 InteliSense模式 改为 gcc-x64 即可
这时我们可以发现文件夹中多了一个 .vscode 的文件夹,证明配置成功
接下来配置三个主要的文件

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/Program/mingw-w64/mingw64/bin/g++.exe",//编译器的路径
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

接下来,创建一个tasks.json文件来告诉VS Code如何构建(编译)程序
该任务将调用g++编译器基于源代码创建可执行文件
按快捷键 Ctrl+Shift+P 调出命令面板,输入 tasks,选择 Tasks:Configure Default Build Task

再选择 C/C++: g++.exe build active file

接下来在 .vscode 文件夹中就会出现 tasks.json 的配置文件
可以参考我的配置:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "g++.exe build active file",
			"command": "D:/Program/mingw-w64/mingw64/bin/g++.exe",//编译器路径
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "D:/Program/mingw-w64/mingw64/bin"
			},//bin为存放编译器和调试器的文件夹
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: D:/Program/mingw-w64/mingw64/bin/g++.exe"
		}
	]
}

launch.json

点击菜单栏的 Debug-->Start Debugging 或者直接按键盘 F5
会在 .vscode 文件夹中生成一个 launch.json 的配置文件
我的配置如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
            "type": "cppdbg",//配置类型,只能为cppdbg
            "request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
            "args": [],//调试传递参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,//true显示外置的控制台窗口,false显示内置终端
            "MIMode": "gdb", 
            "miDebuggerPath": "D:\\Program\\mingw-w64\\mingw64\\bin\\gdb.exe",//调试器路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

运行测试

写一个 hello world! 的程序测试下环境配置是否成功
记得要在末尾插入 system("pause") 的代码,不然程序会闪一下就消失了
出现如图所示结果则成功:

posted @ 2022-06-06 17:12  ThinkGone  阅读(1207)  评论(0编辑  收藏  举报