2022.7.3:复杂项目有待验证!!待更新
在.vscode目录中添加以下三个文件
- launch.json
- settings.json
- tasks.json
这个文件配置也和linux win系统无关,你只需要在编译时告诉vscode使用哪个编译器即可。复杂项目还未测试,请悉知!
launch.json的配置内容
{
// 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": "(gdb) Launch",//配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg",//配置类型
"request": "launch",//请求配置类型,有launch(启动)或attch(附加)两种
"program": "${workspaceFolder}/build/main", //调试时需要指定的程序路径
"args": [],//调试时给程序输入的参数
"stopAtEntry": false,//为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}",//调试程序时的工作目录,${workspaceFolder}表示代码所在目录
"environment": [],
"externalConsole": true,//调试时是否显示控制台窗口,true为在终端显示
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make" //这个重要,task中的make
}
]
}
settings.json内容
{
"files.associations": {
"array": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
}
tesks.json
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceRoot}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-G",
"Unix Makefiles",
"-DCMAKE_BUILD_TYPE=Debug",
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"type": "shell",
"command": "make",
"args": []
}
]
}
怎么使用?
安装插件:
配置CMakeLists.txt
首先,主目录需要写一个CMakeLists.txt,然后每添加一个包含源码的子目录,都需要在子目录下写一个CMakeLists.txt,然后在主目录的CMakeLists.txt文件中包含该子目录。
例如,我的测试项目的整体结构是这样的:
---.vscode
---build
---func//子目录1
---CMakeLists.txt
---func.cpp
---func.h
---lib//生成的库文件存放路径
---test//子目录2
---CMakeLists.txt
---add.cpp
---add.h
---hello.cpp
---hello.h
---CMakeLists.txt
---main.cpp
cmake文件怎么写就不展示了,这就取决于你的项目是如何配置的,当然你也可以把所有源文件放在src目录下,头文件放在include目录下,这都是可以的,关键还是你如何配置你的CMakeLists.txt,因为vscode的配置已经在.vscode中配置好了,你需要告诉cmake编译你主目录下的所有文件!!,如果不知道cmake怎么写的话就学一下吧,基本的东西比较简单,学完之后写个简单的测试项目还是比较有成就感的,这也是学习的乐趣所在!