如何将cppcheck的输出重定向到文件?

std*_*err 5 linux bash redirect stdout cppcheck

我想将cppcheck的输出重定向到一个文本文件。它会打印很多信息,stdout但是如果执行此操作cppcheck --enable=all --verbose . > /srv/samba/share/tmp/cppcheck.out,则不会在文件中获取所有信息,为什么不呢?

orb*_*boy 5

最新的cppcheck开发版本包含一个新选项:

--output-file=<file name>
Run Code Online (Sandbox Code Playgroud)

添加此选项可将输出定向到特定文件。

用法示例:

默认情况下,cppcheck将其结果打印到stdout:

$ cppcheck --enable=all test.cpp 
  Checking test.cpp ...
  [test.cpp:54]: (style) The scope of the variable 'middle' can be reduced.
  (information) Cppcheck cannot find all the include files (use --check-config for details)
Run Code Online (Sandbox Code Playgroud)

您可以如下使用选项--output-file将结果存储在report.txt中:

$ cppcheck --enable=all --output-file=report.txt test.cpp 
Checking test.cpp ...
Run Code Online (Sandbox Code Playgroud)

现在,结果存储在report.txt中:

$ more report.txt 
[test.cpp:54]: (style) The scope of the variable 'middle' can be reduced.
(information) Cppcheck cannot find all the include files (use --check-config for details)
Run Code Online (Sandbox Code Playgroud)

或者,您可以将输出重定向到文件:

$ cppcheck --enable=all test.cpp 2> report.txt
Checking test.cpp ...
Run Code Online (Sandbox Code Playgroud)

现在,结果存储在report.txt中:

$ more report.txt 
[test.cpp:54]: (style) The scope of the variable 'middle' can be reduced.
(information) Cppcheck cannot find all the include files (use --check-config for details)
Run Code Online (Sandbox Code Playgroud)