安装scan-build
安装clang后,scan-build不会自动安装,要手动安装:
sudo apt install clang-tools
下载cmake使用示例:
$ git clone https://github.com/ttroy50/cmake-examples.git
修改示例脚本中scan-build的版本号
查看本机clang版本
$ clang --version
clang version 10.0.0-4ubuntu1
或者使用table补全发现版本号是10
$ scan-build
scan-build scan-build-10 scan-build-py-10
修改run_test.sh中scan-build版本号:
#!/bin/bash
mkdir -p build \
&& cd build \
&& scan-build-10 -o scanbuildout cmake .. \
&& scan-build-10 -o scanbuildout make
执行脚本
输出错误显示:没有安装c++编译器
$ cd cmake-examples/04-static-analysis/clang-analyzer
$ ./run_test.sh
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is unknown
-- Check for working C compiler: /usr/share/clang/scan-build-10/libexec/ccc-analyzer
-- Check for working C compiler: /usr/share/clang/scan-build-10/libexec/ccc-analyzer -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/share/clang/scan-build-10/libexec/c++-analyzer
-- Check for working CXX compiler: /usr/share/clang/scan-build-10/libexec/c++-analyzer -- broken
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCXXCompiler.cmake:53 (message):
The C++ compiler
安装g++
$ sudo apt-get install g++
输出内容
$ ./run_test.sh
....
Scanning dependencies of target subproject1
[ 25%] Building CXX object subproject1/CMakeFiles/subproject1.dir/main1.cpp.o
[ 50%] Linking CXX executable subproject1
[ 50%] Built target subproject1
Scanning dependencies of target subproject2
[ 75%] Building CXX object subproject2/CMakeFiles/subproject2.dir/main2.cpp.o
/home/jiushi/ubuntu/cmake_test/cmake-examples/04-static-analysis/clang-analyzer/subproject2/main2.cpp:7:17: warning: Dereference of null pointer (loaded from variable 'x')
std::cout << *x << std::endl;
^~
1 warning generated.
[100%] Linking CXX executable subproject2
[100%] Built target subproject2
scan-build: 1 bug found.
scan-build: Run 'scan-view /mnt/e/dev/ubuntu/cmake_test/cmake-examples/04-static-analysis/clang-analyzer/build/scanbuildout/2022-02-20-170742-2049-1' to examine bug reports.
详细内容参考下方github链接。build/scanbuildout/2022-02-20-170742-2049-1目录下为html文件