> 文章列表 > VSCode纯手工配置C/C++项目

VSCode纯手工配置C/C++项目

VSCode纯手工配置C/C++项目

面向大二同学不想用Visual Studio的需求,探索Visual Studio Code平台上单纯利用C/C++纯手动配置的方法,实现Release版本和Debug版本的调试和运行,并指定版本进行调试。

前置依赖项:

  • C/C++1 VSCode扩展
  • 配置文件列表,将下面的代码放置到对应目录即可
VSCode纯手工配置C/C++项目

图 1-1 配置文件列表
  • 添加源文件,main.cpp中一定要有main函数,不然你得更改配置文件
VSCode纯手工配置C/C++项目

图 1-2 源文件列表
  • 项目启动,点击启动即可
VSCode纯手工配置C/C++项目

图1-3 启动项
  • 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环境"&&"]}}
    

c_cpp_properties.json

{"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
}

tasks.json

{"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."}]
}

launch.json

{"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}}]
}

  1. https://code.visualstudio.com/docs/languages/cpp ↩︎

  2. https://blog.csdn.net/lichen849/article/details/109626205 ↩︎

  3. https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt ↩︎