Prepare

查看当前环境 C++ 版本:

$ ls -l /lib64/libstdc++.so.6
lrwxrwxrwx 1 root root 19 Aug 18  2020 /lib64/libstdc++.so.6 -> libstdc++.so.6.0.25
$ rpm -qf /lib64/libstdc++.so.6
libstdc++-8.3.1-5.el8.0.2.x86_64

Conventions

Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. These conventions usually cover file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices, programming principles, programming rules of thumb, architectural best practices, etc. These are guidelines for software structural quality. Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Coding conventions are only applicable to the human maintainers and peer reviewers of a software project. Conventions may be formalized in a documented set of rules that an entire team or company follows, or may be as informal as the habitual coding practices of an individual. Coding conventions are not enforced by compilers.

Modern C++

Format

Compiler

Build

-Wpedantic

Issue all the warnings demanded by strict ISO C and ISO C++; diagnose all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. This follows the version of the ISO C or C++ standard specified by any -std option used.

Test

Check

Clang 项目也提供了其他一些工具,包括代码的静态检查工具 Clang-Tidy 。这是一个比较全面的工具,它除了会提示你危险的用法,也会告诉你如何去现代化你的代码。默认情况下,Clang-Tidy 只做基本的分析。你也可以告诉它你想现代化你的代码和提高代码的可读性:

clang-tidy --checks='clang-analyzer-*,modernize-*,readability-*' test.cpp
  • Clang-Tidy 还是一个比较“重”的工具。它需要有一定的配置,需要能看到文件用到的头文件,运行的时间也会较长。而 Cppcheck 就是一个非常轻量的工具了。它运行速度飞快,看不到头文件、不需要配置就能使用。它跟 Clang-Tidy 的重点也不太一样:它强调的是发现代码可能出问题的地方,而不太着重代码风格问题,两者功能并不完全重叠。有条件的情况下,这两个工具可以一起使用。

  • Valgrind 算是一个老牌工具了。它是一个非侵入式的排错工具。根据 Valgrind 的文档,它会导致可执行文件的速度减慢 20 至 30 倍。但它可以在不改变可执行文件的情况下,只要求你在编译时增加产生调试信息的命令行参数(-g),即可查出内存相关的错误。

int main()
{
  char* ptr = new char[20];
}

在 Linux 上使用 g++ -g test.cpp 编译之后,然后使用 valgrind --leak-check=full ./a.out 检查运行结果,得到的输出会如下所示:

valgrind

即其中包含了内存泄漏的信息,包括内存是从什么地方泄漏的。Valgrind 的功能并不只是内存查错,也包含了多线程问题分析等其他功能。要进一步了解相关信息,请查阅其文档。

  • nvwa 项目里,我也包含了一个很小的内存泄漏检查工具。它的最大优点是小巧,并且对程序运行性能影响极小;缺点主要是不及 Valgrind 易用和强大,只能检查 new 导致的内存泄漏,并需要侵入式地对项目做修改。
c++ test.cpp \../nvwa/nvwa/debug_new.cpp

Why is clang-tidy in clangd so much faster than (run-)clang-tidy itself?

Getting started with Infer

Debug

  • Build your tests with STL in debug mode:
    • libstdc++: -D_GLIBCXX_ASSERTIONS or -D_GLIBCXX_DEBUG.
    • libc++: -D_LIBCPP_DEBUG=1

_GLIBCXX_DEBUG

Undefined by default. When defined, compiles user code using the debug mode. When defined, _GLIBCXX_ASSERTIONS is defined automatically, so all the assertions enabled by that macro are also enabled in debug mode.

Tools

  • 模版实例化工具:https://cppinsights.io/
  • 编译运行工具:https://wandbox.org/
  • 代码统计工具 (Count Lines of Code):https://github.com/AlDanial/cloc

Manual

Posts

Common Libraries

Refer

  • https://rigtorp.se/cpp-best-practices/
  • https://lefticus.gitbooks.io/cpp-best-practices/content/
  • O’Reilly video: Learning C++ Best Practices