且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CMake中使用cpplint代码样式检查?

更新时间:2022-06-13 01:16:07

Daniel Pffeifer的"Effective Cmake"(

Recommended way to use static analysis tools with CMake was presented in Daniel Pffeifer's "Effective Cmake" (https://www.***.com/watch?v=rLopVhns4Zs&amp=&t=77m13s).

您可以在调用 cmake 时定义它,例如:

You can either define it when calling cmake, eg.:

cmake "-DCMAKE_CXX_CPPLINT=cpplint" ..

或将其放入 CMakeLists.txt :

set(CMAKE_CXX_CPPLINT "cpplint")

推荐选项是第一个(我们不应该在项目中定义非项目要求的内容).

Recommended option is the first one (we shouldn't define in a project what isn't a project requirement).

CMake将为其编译的每个文件调用 cpplint .您可以在分号后传递其他参数(例如 -DCMAKE_CXX_CPPLINT = cpplint;-linelength = 100 ).

CMake will call cpplint for each file it compiles. You can pass extra arguments after semicolon (e.g. -DCMAKE_CXX_CPPLINT=cpplint;--linelength=100).

此方法的缺点:

  1. 错误计数将不会累积(因为分别为每个文件调用了 cpplint ).
  2. 它不会检查头文件(与D. Pffeifer在其演示文稿中所说的相反,包含文件未由 cpplint 进行扫描)
  1. Errors count will not get accumulated (because cpplint is invoked for each file separately).
  2. It will not check header files (as opposed to what D. Pffeifer says in his presentation, include files are not being scanned by cpplint).


请注意,您可以以相同的方式使用其他静态分析工具:


Note that you can use other static analysis tools the same way:

  • 氏族整洁-DCMAKE_CXX_CLANG_TIDY =/usr/bin/clang-tidy-3.9; -checks = *"
  • CppCheck -DCMAKE_CXX_CPPCHECK =/usr/bin/cppcheck;-std = c ++ 11"
  • IWYU -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE =/usr/bin/iwyu;-transitive_includes_only"
  • LWYU cmake -DCMAKE_LINK_WHAT_YOU_USE = TRUE
  • 懒散

其中一些将需要编译数据库"(设置(CMAKE_EXPORT_COMPILE_COMMANDS ON)).

Some of them will require "compilation database" (set(CMAKE_EXPORT_COMPILE_COMMANDS ON)).