??? 0. 背景 ?? 为了方便多人协作,大家都会尽可能保证程序风格统一,人工检查效率比较低,一般会借助程序工具自动做一些检查,cpplint是Google开发的一个C++代码风格检查工具,如果是遵循google code style的,可以使用cpplint作为代码规范的一个检查工具.
sudo python3 -m pip install cpplint
$ which cpplint
/usr/local/bin/cpplint
$ cpplint --version
Cpplint fork (https://github.com/cpplint/cpplint)
cpplint 1.6.0
Python 3.6.9 (default, Mar 18 2022, 13:55:28)
[GCC 8.4.0]
1. 参考
????????使用cpplint检查符合谷歌风格的header guard,一个个手动编写效率很低,可以安装vscode插件C/C++ Snippets,参考官方文档:Snippets in Visual Studio Code 和博客VSCode 自动生成头文件的#ifndef #define #endif_Static H的博客-CSDN博客_vscode生成头文件
?以及regex - Implement VSCode snippet for C++ header guard - Stack Overflow
自己编写了符合谷歌风格的header guard
2. 使用
??????? 安装完插件,配置json文件 (Help→Show All Commands)Ctrl+Shift+p 输入Snippets, 选择Preferences: Configure User Snippets选择cpp,将下面内容粘贴到cpp.json保存即可,同时还配置了生成namespace的快捷键
{
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"C C++ Header": {
"scope": "c, cpp",
"prefix": "hd",
"description": "Add #ifndef, #define and #endif",
"body": [
"#ifndef ${RELATIVE_FILEPATH/([^a-zA-Z0-9])|([a-zA-Z0-9])/${1:+_}${2:/upcase}/g}_",
"#define ${RELATIVE_FILEPATH/([^a-zA-Z0-9])|([a-zA-Z0-9])/${1:+_}${2:/upcase}/g}_",
"",
"$0",
"#endif // ${RELATIVE_FILEPATH/([^a-zA-Z0-9])|([a-zA-Z0-9])/${1:+_}${2:/upcase}/g}_",
""
]
},
"C C++ namespace": {
"scope": "c, cpp",
"prefix": "ns",
"description": "Add namespace ",
"body": [
"namespace ${TM_FILENAME_BASE/(.*)/${1:/lowcase}/} {",
"$0",
"",
"} // namespace ${TM_FILENAME_BASE/(.*)/${1:/lowcase}/}",
""
]
}
// 参考链接:https://blog.csdn.net/weixin_45461426/article/details/105936955
// 官网 https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-syntax
// https://stackoverflow.com/questions/65156083/implement-vscode-snippet-for-c-header-guard?rq=1
}
3. 测试
??????? 新建一个cpp文件,输入"hd" 和"ns"然后回车即可如下所示:
#ifndef SRC_SNIPPLE_TEST_SRC_TEST_SNIPPLE_H_
#define SRC_SNIPPLE_TEST_SRC_TEST_SNIPPLE_H_
namespace test_snipple {
} // namespace test_snipple
#endif // SRC_SNIPPLE_TEST_SRC_TEST_SNIPPLE_H_
|