1、安装MinGW/GCC编译器
2、配置环境变量 比如 E:\MinGW\bin;
3、安装vscode
在cmd/powershell,输入code .
,可以打开vscode
4、安装vscode插件
打开vscode,按ctrl+p打开快速命令框,输入以下命令后等待
ext install cpptools
下载安装插件C/C++、C++ Intellisense
5、配置.vscode文件
第一,打开vscode,打开文件夹,新建一个文件夹,起名字,比如vscode,新建一个文件,起名字,比如test.cpp
#include <iostream> using namespace std; int main() { cout<<"Hello"; system("pause"); return 0; }
第二,按ctrl+shift+d,点击那个带着红点的齿轮,选择C++(GDB/LLDB),选择default configuration ,然后会在.vscode目录下,生成一个launch.json
的启动配置文件,修改如下:
{ "version": "0.2.0", "configurations": [ { "name": "(gdb)Launch", "type":"cppdbg", "request": "launch", "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe", // "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe", //miDebuggerPath是环境变量的路径,64位gdb.exe "preLaunchTask": "g++", // "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
第三,再在文件夹中新建tasks.json
文件
{ "version": "2.0.0",//版本号 "command": "g++", "args": ["-g","${file}","-o","${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 } } }
注:多文档 1、包含自定义头文件可以使用相对路径,右键点击头文件可以复制路径; 2、.cpp 文件的有效使用,"args" 里的 "${file}", 要修改,例如:
"${workspaceFolder}/test/*.cpp", //需要包含的cpp "${workspaceFolder}/main.cpp", //同上
6、调试.cpp
注:因为VS需要为每一个文件夹做单独配置,所以建议把.vscode
文件夹放到你常用的文件夹的顶层,这样就不用重复配置了
附:
c_cpp_properties.json文件
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/include/*" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "intelliSenseMode": "msvc-x64" } ], "version": 4 }
终端将被任务重用,按任意键关闭,这个提示的原因是本地端口是被共享的,所以必须关闭一个进程才能执行另一个,只需要对 tasks.json
文件做一些修改即可
{ "version": "2.0.0", "command": "g++", "args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"], // 编译命令参数 "problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "${workspaceFolder}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared", //这里shared表示共享,改成new之后每个进程创建新的端口 "showReuseMessage": true, "clear": false } }