SonarQube+cppcheck实现C++代码扫描

背景

最近接收到C++代码检测的需求,一开始上网找了很久,sonar的检测都是扫描java、web等,很少有C++的,现将自己成功搭建和测试的结果记录于此。

工具准备

  • SonarQube工具:展示扫描报告,版本7.6(SonarQube 7.9以上版本不再支持mysql)
  • sonar_scanner工具:代码扫描工具
  • Cppcheck工具:Cppcheck是一种C/C++代码缺陷静态检查工具,不同于C/C++编译器及其它分析工具,Cppcheck只检查编译器检查不出来的bug,不检查语法错误。

安装步骤

1、sonar我们可以去官网直接下载Download | SonarQube ,下载解压之后显示的是如下的目录结构

[sonar@bogon sonarqube-7.1]$ ls
bin  conf  COPYING  data  elasticsearch  extensions  lib  logs  temp  web

2、SonarQube的默认C/C++插件CFamily是收费的,但有个作者自己写了个开源的cxx插件,下载地址https://github.com/SonarOpenCommunity/sonar-cxx/releases,把jar文件下载下来,然后放到你的sonarqube目录/extensions/plugins目录下

[sonar@bogon plugins]$ ls
sonar-csharp-plugin-7.0.1.4822.jar      sonar-l10n-zh                        sonar-scm-svn-plugin-1.7.0.1017.jar
sonar-cxx-plugin-1.1.0.jar              sonar-l10n-zh-plugin-1.21.jar        sonar-typescript-plugin-1.6.0.2388.jar
sonar-flex-plugin-2.4.0.1222.jar        sonar-php-plugin-2.13.0.3107.jar     sonar-xml-plugin-1.5.0.1373.jar
sonar-java-plugin-5.6.0.15032.jar       sonar-python-plugin-1.9.1.2080.jar
sonar-javascript-plugin-5.1.1.7506.jar  sonar-scm-git-plugin-1.4.0.1037.jar

多了个sonar-cxx-plugin-1.1.0.jar的插件

3、进入到sonar的解压目录中的conf中,找到sonar.properties文件中 ,修改里面配置数据库信息(依据自己的数据库修改,Mysql版本不要使用8.0以上的,不然后续sonar服务会启动不起来,具体原因未知,当时配了好几次才发现数据库版本问题导致服务启动失败)

sonar.jdbc.username=root
sonar.jdbc.password=Temp#1234
sonar.sorceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonardb?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=true

4、mysql里新增数据库

CREATE DATABASE sonardb DEFAULT CHARACTER SET utf8;

5、进入bin目录下启动服务(根据自己的系统选择对应的启动脚本,我的是Linux_64)

[sonar@bogon sonarqube-7.1]$ cd bin/
[sonar@bogon bin]$ ls
jsw-license  linux-x86-32  linux-x86-64  macosx-universal-64  windows-x86-32  windows-x86-64
[sonar@bogon bin]$ cd linux-x86-64/
[sonar@bogon linux-x86-64]$ ls
lib  SonarQube.pid  sonar.sh  wrapper
[sonar@bogon linux-x86-64]$ ./sonar.sh start

6、界面访问

​ http://10.206.142.87:9000(默认登录账号密码都是admin)

登录之后点击“代码规则”,便可在页面看到“C++ (Community)”

7、修改质量配置

sonar平台质量配置,新增C++的质量配置项

激活更过规则

激活完成后,将该规则项设置为默认。

8、安装Cppcheck和sonar-scanner

wget https://github.com/danmar/cppcheck/archive/1.89.zip
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip

下载完后,解压出来,并重新命名sonar_scanner和配置sonar.properties

#重命名为sonar-scanner
mv sonar-scanner-cli-3.0.3.778-linux sonar-scanner
# 修改sonar_scanner.properties
[sonar@bogon ~]$ cd sonar-scanner/conf/
[sonar@bogon conf]$ ls
sonar-scanner.properties
[sonar@bogon conf]$vim sonar-scanner.properties
#把注释去掉就行
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8

最后将添加sonar、cppcheck、sonar-scanner添加到环境变量中

vim /etc/profile

#添加一下代码(根据自己路径修改)
export SONAR_HOME=/home/sonar/sonarqube-7.1
export SONAR_SCANNER_HOME=/home/sonar/sonar-scanner
export CPPCHECK_HOME=/home/sonar/cppcheck-1.88
export PATH=$PATH:$SONAR_HOME:$SONAR_SCANNER_HOME/bin:$CPPCHECK_HOME

source /etc/profile  

9、到此sonar的安装,已全部完成,接下来我们写个例子测试下吧

[chenpk@/home/chenpk/sippro]$vim sonar-project.properties
sonar.projectKey=sippro    #sonar平台中相对应项目的key
sonar.projectName=sippro   #sonar平台中相对应项目的名字
sonar.projectVersion=1.0   #sonar平台中相对应项目的项目版本
sonar.sources=./           #sonar检测的源文件目录,‘.’表示当前根目录下的所有文件目录
sonar.language=c++         #sonar检测的编程语言种类
sonar.sourceEncoding=UTF-8 #sonar平台中相对应项目的编码格式

[chenpk@/home/chenpk/sippro]$ls
build  cmake  CMakeLists.txt  eva  han  help  muduo  MysqlInterface  README.md  sonar-project.properties

执行sonar-scanner命令进行检测

[chenpk@/home/chenpk/sippro]$sonar-scanner
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/chenpk/sippro/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-862.el7.x86_64 amd64
INFO: User cache: /home/chenpk/.sonar/cache
INFO: SonarQube server 7.1.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=193ms
INFO: Server id: AW0e9nx7Z8oUve48CK6d
INFO: User cache: /home/chenpk/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=98ms
INFO: Load/download plugins
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
.......
INFO: 93 files had no CPD blocks
INFO: Calculating CPD for 150 files
INFO: CPD calculation finished
INFO: Analysis report generated in 690ms, dir size=3 MB
INFO: Analysis reports compressed in 610ms, zip size=1 MB
INFO: Analysis report uploaded in 333ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://10.206.142.87:19000/dashboard/index/sippro
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://10.206.142.87:19000/api/ce/task?id=AW0fYZa2lfgfkyZw33vG
INFO: Task total time: 19.459 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 21.966s
INFO: Final Memory: 98M/771M
INFO: ------------------------------------------------------------------------

检测完之后,登录平台查看

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
tolua++是一个将C/C++代码导出到Lua的工具,它可以将C/C++代码转换成Lua解释器调用的API,并在运行时将C/C++代码封装成Lua可用的函数。 以下是一个简单的示例,演示如何使用tolua++将C++代码转换成Lua可调用的库: ```c++ // example.h class Example { public: Example(int value); int getValue(); private: int m_value; }; ``` ```c++ // example.cpp #include "example.h" Example::Example(int value) : m_value(value) { } int Example::getValue() { return m_value; } ``` 首先,我们需要使用tolua++生成绑定代码。假设我们的绑定代码文件名为examplebindings.pkg,其内容如下: ``` // examplebindings.pkg $#include "example.h" module example { class Example { public: Example(int value); int getValue(); }; } ``` 然后,我们可以使用tolua++命令行工具将绑定代码生成为C++头文件和Lua文件: ``` $ tolua++ -o examplebindings.h -H examplebindings.hpp examplebindings.pkg ``` 生成的examplebindings.h和examplebindings.hpp文件包含了C++的绑定代码,我们需要将其与原始的C++代码一起编译成库文件。 接下来,我们可以编写Lua脚本来调用C++代码: ```lua -- example.lua require "examplebindings" local example = example.Example(42) print(example:getValue()) -- 输出42 ``` 在上面的示例中,我们首先加载了examplebindings模块,然后创建了一个Example对象,并调用了其getValue()方法。注意,我们使用了冒号语法来调用方法,这是Lua中常用的一种语法。 最后,我们可以使用Lua解释器来运行example.lua脚本,以调用C++代码并输出结果。 以上就是使用tolua++将C++代码转换成Lua可调用的库的基本流程。需要注意的是,tolua++只支持部分C++特性,例如模板和多重继承等特性可能无法正确导出。在实际使用中,我们需要根据具体情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

orgotF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值