工欲善其事,必先利其器
安装插件:
要用 VS Code 比较方便的写 C/C++,需要满足以下三个要求:
–. 编译器并设好环境变量,推荐 MinGW-w64
–. VS Code 的C/C++ 插件:
–. VS Code 的 Code Runner 插件
其他
–. c++ Intllisense
–. Bracket Pair Colorizer2 :彩虹花括号。
–. gitLens:
–. tabout:t按一下tab键直接从括号或者引号中跳出.
–. cmake:
–. One Dark Pro:主题
–. Clang-Format:定义风格
–. CodeLLDB:待定
–. c/c++ Snippets:待定
用户配置:
.vscode 文件夹下
–. c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"compilerPath": "C:\\MinGW\\bin\\g++.exe",
"includePath": [
"${workspaceFolder}"
],
"defines": [],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
–. launch.json
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,我一般设置为true
"cwd": "${workspaceFolder}", // 调试程序时的工作目录
"environment": [], // (环境变量?)
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但目前lldb在windows下没有预编译好的版本。
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则去掉
"preLaunchTask": "build" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}
–. setting.json
{
"files.associations": {
"iostream": "cpp"
}
}
–. tasks.json
// https://code.visualstudio.com/docs/editor/tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "build", // 任务名称,与launch.json的preLaunchTask相对应
"command": "g++", // 要使用的编译器
"args": [
"-g",// 生成和调试有关的信息
"${file}",
"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
"${fileDirname}\\${fileBasenameNoExtension}.exe",
//"${fileDirname}/${fileBasenameNoExtension}.exe",
], // 编译命令参数
"type": "shell", // 可以为shell或process,前者相当于先打开shell再输入命令,后者是直接运行命令
"group": {
"kind": "build",
"isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
},
"problemMatcher":{
"owner": "$gcc",
"fileLocation":"absolute",
"pattern":[
{
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
//"location": 2,
"message": 5
}
]
}
}
]
}
linux:
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
–. tasks.josn
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-l",
"pthread"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
]
}
]
}
配置说明
一般情况下,c++的debug过程是这样的,首先vscode调用launch.json, launch.json根据"preLaunchTask": “gcc build active file”, 的名称调用名为"gcc build active file" 的task.json, task.json中可以有多个task,根据tasks中的label名调用对应的任务。task主要负责进行编译成可执行文件。如果仅仅需要生成可执行文件,直接按crtl+shift+b即可。
c_cpp_properties主要是针对c/c++编译进行的专门配置,如include路径等。
强烈建议看json中的英文,其描述是非常容易理解的。对c++的编译过程、对Linux稍微有些基础的人都能够快速掌握并进行私人定制。
打开VSC,选打开文件夹。
ps:include 与 智能填充
有时候会用VScode来组件C/C++工程,并且用到了一些外部依赖的库文件,比如OpenCV。此时希望VSCode的代码提示功能能够提示OpenCV中的函数,这时候就需要配置工作空间中的C/C++编译环境。
如果你使用过sublime或者VSCode,就知道Ctrl+Shift+P可以调出控制窗口,那么先按下Ctrl+Shift+P:
再输入edit或者configuration,选择”C/Cpp:Edit Configurations”:
之后会在你的工作空间生成./.vscode/c_cpp_properties.json文件,我们需要做的就是配置这个文件的参数;
在“includePath”的属性中添加你的库文件的地址就行了,类似:
"/home/**/Tools/opencv/cpu-install/include",
"/home/**/Tools/opencv/gpu-install/include"
配置好后,最好重启一下客户端,VSCode就也会给你自动提示opencv库中的函数或类等等的了