面向大二同学不想用Visual Studio的需求,探索Visual Studio Code平台上单纯利用C/C++纯手动配置的方法,实现Release版本和Debug版本的调试和运行,并指定版本进行调试。
前置依赖项:
- C/C++1 VSCode扩展
- 配置文件列表,将下面的代码放置到对应目录即可
- 添加源文件,main.cpp中一定要有main函数,不然你得更改配置文件
- 项目启动,点击启动即可
- cl.exe无法启动问题
1.使用脚本打开vscode目录2
2.使用配置文件配置3"windows": { "options": { "shell": { "executable": "cmd.exe", "args": [ "call", "\"D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/Tools/VsDevCmd.bat\"", //记得自定义> 自己的VsDevCmd环境 "&&" ] } }
{
"configurations": [
{
"name": "windows-msvc-x64",
// 头文件目录后面有需要自行更改
"includePath": ["${workspaceFolder}/**"],
// 自定义编译器路径
"compilerPath": "D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "windows-msvc-x64",
"compilerArgs": [""],
"defines": ["UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS"]
}
],
"version": 4
}
{
"version": "2.0.0",
// Windows启动项目预设值
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"call",
"\"D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/Tools/VsDevCmd.bat\"", //记得自定义自己的VsDevCmd环境
"&&"
]
}
}
},
"tasks": [
// Debug版本
{
"type": "shell",
"label": "C/C++:Debug",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${workspaceFolder}\\build\\Debug\\${fileBasenameNoExtension}.exe",
"${workspaceFolder}\\source\\*.cpp",
"${workspaceFolder}\\main.cpp"
],
"options": {
"cwd": "${workspaceFolder}\\build\\Debug"
},
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
// Release版本
{
"type": "shell",
"label": "C/C++:Release",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${workspaceFolder}\\build\\Release\\${fileBasenameNoExtension}.exe",
"/DNDEBUG",
"${file}",
"user32.lib",
"kernel32.lib",
"shell32.lib"
],
"options": {
"cwd": "${workspaceFolder}\\build\\Release"
},
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
{
"version": "0.2.0",
"configurations": [
{
// 对应cl.exe
"name": "msvc",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\build\\Debug\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"environment": [],
"console": "integratedTerminal",
"preLaunchTask": "C/C++:Debug",
"logging": {
// 用于消除PDB文件找不到打不开问题,来自于https://none53.hatenablog.com/entry/2019/11/28/vsCode_Cannot_find_or_open_the_PDB_file.
"moduleLoad": false
}
}
]
}
https://code.visualstudio.com/docs/languages/cpp ↩︎
https://blog.csdn.net/lichen849/article/details/109626205 ↩︎
https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt ↩︎