Visual Studio Code配置C/C++编译运行环境
1、下载安装vscode
https://code.visualstudio.com/Download
2、安装cpptools插件
vscode列出插件列表,如图:

配置IntelliSence
在VS Code中打开,然后在VS Code中按Ctrl + Shift + P输入命令C/C++: Edit Configurations...,VS Code会自动在项目文件夹根目录新建配置文件夹.vscode并同时在.vscode中新建配置文件c_cpp_properties.json。
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:/mingw64/bin/gcc.exe", //修改为本机gcc.exe路径 "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
配置编译选项
在.vscode文件夹中新建文件tasks.json进行配置。先给出配置文件内容:
{ "version": "2.0.0", "tasks": [ { "label": "Build", "command": "C:/mingw64/bin/g++.exe", // 修改这里为本机g++路径 "type": "shell", "args": [ "-g", "-Wall", "-std=c++11", "-lm", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" //Linux和macOS这里要修改为.o ], "presentation": { "reveal": "always", "echo": true, "focus": true }, "problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "${workspaceRoot}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": { "kind": "build", "isDefault": true } }, { "label": "Run", "type": "shell", "dependsOn": "Build", "command": "${fileDirname}/${fileBasenameNoExtension}.o", "windows": { "command": "${fileDirname}/${fileBasenameNoExtension}.exe" }, "args": [], "presentation": { "reveal": "always", "focus": true }, "problemMatcher": [], "group": { "kind": "test", "isDefault": true } } ] }
配置Debug选项
如果要启用Debug,需要在.vscode文件夹下创建launch.json。
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示 "type": "cppdbg",// 配置类型,这里只能为cppdbg "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加) "program": "${fileDirname}/${fileBasenameNoExtension}.o", // 将要进行调试的程序的路径 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "externalConsole": true, "environment": [], "MIMode": "gdb", "windows": { "program": "${fileDirname}/${fileBasenameNoExtension}.exe", "miDebuggerPath": "C:/Dev/MinGW/mingw64/bin/gdb.exe" // 修改为本机gdb.exe的路径 }, "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "Build" } ] }
3、安装编译、调试环境
使用的是MinGW. 下载解压,配置环境变量,在cmd中输入g++ --version 与gcc --version进行验证
4、修改vscode调试配置文件
打开文件夹后,新建test.c进行测试:
按Ctrl + Shift + P然后输入Run Build Task即可编译。输入Run Test Task即可运行。在设置中可以为这两个选项添加快捷键绑定。按Ctrl + Shift + F可以格式化代码。
立志如山 静心求实
浙公网安备 33010602011771号