VSCode编译调试c/c++文件
程序员文章站
2022-04-02 18:52:57
...
VSCode编译调试c/c++文件
一.下载wingw64免安装版WINGW64免安装版
二.将其解压到合适的位置。
三.添加vscode插件
- C/C++
- Code Runner
四.创建一个存放代码的文件夹,并在VScode的资源管理器中–》右键–》”将文件夹添加到工作区“
五.下载配置文件C-C++配置文件解压后将里面的”.vscode“文件夹复制到上面创建的文件夹里。
六.修改配置文件
1.修改 c_cpp_properties.json 文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
//将下面的地址修改为你自己的wingw64的存放位置,注意单斜杠改为双斜杠
"compilerPath": "C:\\C_C++\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
2.修改 launch.json 文件
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
//修改为你自己的地址,注意斜杠的方向哦!!!!
"miDebuggerPath": "C:/C_C++/mingw64/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
},
]
}
3.tasks.json 文件内容如下,无需修改
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"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
}
}
4.配置好后如下
4. 按下 ”ctrl+shift+p“输入”settings.json“选择—》”首选项:打开设置(json)“
添加如下代码:
//code run插件
"code-runner.executorMap": {
"c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
},//若不加c,cpp在utf-8下编译乱码
"editor.formatOnType": true, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
"code-runner.runInTerminal": true,
"code-runner.clearPreviousOutput": true,
"code-runner.saveAllFilesBeforeRun": true,
"debug.onTaskErrors": "debugAnyway",
七.在创建的工作区内编写cpp文件
八.在代码编辑器右键–>Run Code(crtl+alt+n)即可编译运行。
九.调试cpp文件,在”瓢虫“里选择C/C++Debug(C-C++),然后按下F5即可。