2022.7.3:复杂项目有待验证!!待更新
在.vscode目录中添加以下三个文件
- launch.json
- settings.json
- tasks.json
这个文件配置也和linux win系统无关,你只需要在编译时告诉vscode使用哪个编译器即可。复杂项目还未测试,请悉知!
launch.json的配置内容
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "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
---CMakeLists.txt
---func.cpp
---func.h
---lib
---test
---CMakeLists.txt
---add.cpp
---add.h
---hello.cpp
---hello.h
---CMakeLists.txt
---main.cpp
cmake文件怎么写就不展示了,这就取决于你的项目是如何配置的,当然你也可以把所有源文件放在src目录下,头文件放在include目录下,这都是可以的,关键还是你如何配置你的CMakeLists.txt,因为vscode的配置已经在.vscode中配置好了,你需要告诉cmake编译你主目录下的所有文件!!,如果不知道cmake怎么写的话就学一下吧,基本的东西比较简单,学完之后写个简单的测试项目还是比较有成就感的,这也是学习的乐趣所在!
|