资讯详情

Ubuntu下cppcheck的安装和使用

一、cppcheck的安装

# DESTDIR 目的路径安装 # FILESDIR 实际安装路径为`DESTDIR FILESDIR`,注意这两条路径的末尾斜杠都要去掉 make -j8 FILESDIR=/usr/share/cppcheck DESTDIR= MATCHCOMPILER=yes sudo make install FILESDIR=/usr/share/cppcheck DESTDIR= MATCHCOMPILER=yes

二、cppcheck的使用 简述:cppcheck 是一种 C/C 静态检查工具代码缺陷。 C/C 编译器和许多其他分析工具不检查代码中的语法错误。Cppcheck 只检查编译器检查不出来的东西 bug 类型的目的是检查代码中的真实错误。

部分操作介绍,如需更多操作说明,请使用cppcheck --help进行查看

1、检查某一个c/c 文件

cppcheck file.c

2.检查文件夹中的所有文件

cppcheck path

cppcheck将递归检查path所有源文件都夹在文件夹中

3、启用消息

在默认情况下,只显示错误信息,可以通过–enable命令使用更多的检查

使用警告信息:

cppcheck --enable=warning file.c

启用性能信息:

cppcheck --enable=performance file.c

启用信息消息:

cppcheck --enable=information file.c

由于历史原因 --enable=style 警告、性能、可移植性和样式信息可以使用。使用旧的 XML 在格式上,这些都是由 style 表示:

cppcheck --enable=style file.c

使用警告和性能信息:

cppcheck --enable=warning,performance file.c

启用unusedFunction检查

cppcheck --enable=unusedFunction file.c

启用所有消息:

cppcheck --enable=all

4.将检查结果保存在文件中

cppcheck file.c 2> result.txt

5.多线程检查

cppcheck -j 4 path

多线程检查时,不会对ubusedFunction进行检查

6、CMake项目cppcheck检查的使用

首先,在CMakeList.txt通过同一目录cmake生成项目的compile_commands.json文件

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

然后就可以用了cppcheck检查项目

cppcheck --project=compile_commands.json

检查结果可以通过重定向导入文件

7.屏蔽某些错误

屏蔽syntax error: 命令行参数 --suppress=syntaxError

cppcheck --suppress=memleak:src/file1.cpp src/

8、使用suppressions.txt 统一屏蔽,--suppressions-list=suppressions.txt

示例:

noExplicitConstructor // suppress all noExplicitConstructor errors in all files

// suppress memleak and exceptNew errors in the file src/file1.cpp

memleak:src/file1.cpp

exceptNew:src/file1.cpp

格式:

[error id]:[filename]:[line]

[error id]:[filename2]

[error id]

修改常见错误 (1)隐式结构问题

示例: (style) Class 'Slice' has a constructor with 1 argument that is not explicit.

解决方法:在Slice构造函数前加上explicit,当然,这种结构有时不必显示

(2)变量的初始化

示例:(warning) Member variable 'TableFileCreationInfo::file_size' is not initialized in the constructor.

解决方案:将变量初始值添加到构建函数中

(3)未使用变量/函数

示例:(style) Unused variable: output

示例:(style) The function 'rocksmt_wal_iter_status' is never used.

解决方案:考虑后期是否需要,不需要及时删除,需要保留

(4)raw loop问题

示例:(style) Consider using std::fill algorithm instead of a raw loop. [useStlAlgorithm]

示例:(style) Consider using std::transform algorithm instead of a raw loop. [useStlAlgorithm]

解决方案:将循环便利取代为STL标准库算法函数

(5)引用传输问题

示例:(performance) Function parameter 'f' should be passed by reference.

解决方案:在声明中function尽量使用引用传递,尽量避免值传递

(6)const参数问题

示例:(performance) Function parameter 's' should be passed by const reference. [passedByValue]

解决方案:形参s前添加const,函数中未修改的变量应尽可能声明为const

标签: acpp060310kbe电阻

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司