本文章为个人搭建C++学习环境的学习记录,仅供参考。
1、安装VS2022,勾选使用C++的桌面开发
2、安装CMake
3、安装VsCode
Visual Studio Code - Code Editing. Redefined
4、安装VsCode插件
5、新建你自己的C++工程
6、添加task.json
这个文件是告诉VsCode如何编译当前工程
{
"version": "2.0.0",
"tasks": [
{
//使用本地默认编译器编译cmake
"type": "shell",
"label": "cmake",
"command": "cmake -B ./build ."
},
{
//使用本地默认编译器编译cmake生成的工程
"type": "shell",
"label": "make",
"command": "cmake --build ./build --config=Debug --target=install"
},
{
//依次执行前面两个步骤
"label": "build",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
]
}
]
}
7、添加launch.json
这个文件是告诉VsCode如何运行你的工程
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "msvc",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/build/build/test.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"console": "internalConsole",
"preLaunchTask": "build",
"logging": {
"moduleLoad": false
}
}
]
}
8、选择编译器
按Ctrl + Shift + P打开命令搜索,搜索CMake:Select a Kit,选择你安装的Vs对于的MSVC编译器版本
9、运行你的工程
做完以上配置之后就可以在代码中下断点并运行你的程序。