【window】cpplint在vscode中安装与使用

1.cpplint简介

Cpplint是一个Python脚本,作为一款开源免费的代码静态检测工具,Google也使用它作为自己的C++代码检测工具,也就是说,只要你想代码遵从Google
C++代码规范,那么Cpplint将会提供很好的代码静态检测支持。

如果写c/c++想要遵循良好的代码规范,又希望有工具提示自己是否遵循了规范,那cpplint肯定是一相对完美的答案。之所以说相对,毕竟静态代码检查工具肯定会有漏报,误报,但有一完整的开源代码规范加上一一对应的静态代码检测工具就已经是很不错的选择了

Google C++代码规范链接:https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/contents/

2.cpplint安装

2.1 安装anaconda

window下安装anaconda可以参考:https://zhuanlan.zhihu.com/p/61639212

2.2 安装cpplint

打开cmd,输入命令:

pip install cpplint

注:很安装容易失败,解决方法,有梯子就挂梯子,没梯子改源
改清华镜像源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

conda config --set show_channel_urls yes

2.3 vscode安装cpplint插件

参考下图

2.4 vscode配置cpplint插件

vscode配置cpplint路径,pip install安装后的文件在"\anaconda\Scripts"里面。关于自定义规则的一些想法:pip install安装的源文件来自:https://github.com/cpplint/cpplint,若要自定义规则应该可以通过修改源文件再本地安装来实现。

3. cpplint在vscode中的使用

源码:

#include <stdio.h>
#include"fun.h"

int main()
{
    printf("fun return %d\n", fun(1));
    return 0;
}

cpplint提示:

第1个警告可以参考《Google C++代码规范》中的 1.5. #include 的路径及顺序
第2个警告可以参考《Google C++代码规范》中的 9.4. 函数声明与定义

修改后代码

#include <stdio.h>
#include"src/fun.h"

int main() {
    printf("fun return %d\n", fun(1));
    return 0;
}

如果第一调警告你觉得是误报,可以修改成

#include <stdio.h>
#include"fun.h" // NOLINT

int main() {
    printf("fun return %d\n", fun(1));
    return 0;
}

但如果要屏蔽一种类型的检测建议参考下面操作。

4. cpplint在vscode中的参数配置

首先可以先了解一些cpplint有哪些参数可以配置,这方面可以参考:https://cloud.tencent.com/developer/article/1494003

在vscode中可配置的参数可参考插件简介页面

这里尝试屏蔽"build/include_subdir"类型警告,先打开cpplint设置,选择Filters在settings.json中编辑

修改:

{
    "cpplint.cpplintPath": "D:\\Program Files (x86)\\anaconda\\Scripts\\cpplint.exe", 
    "cpplint.filters": ["-build/include_subdir"]
}

"build/include_subdir"类型警告便会被屏蔽(修改后警告可能不会刷新,重启vscode便可以验证是否屏蔽成功)。

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐