The inclusion order of the header files for the Google C + + programming style guide

Source: Internet
Author: User

The order of the Google C + + programming style header file contains the following instructions:

(1) In order to enhance readability and avoid implied dependencies, the following sequence should be used: C standard library, C + + standard library, header files for other libraries, and your own project header files. However, this first contains the preferred header file, i.e. the A.cpp file should have a precedence of a.h. The preferred header file is to reduce the hidden dependency while ensuring that the header file and the implementation file are matched. The specific example is: If you have a CC file (the Linux platform cpp file suffix cc) is google-awesome-project/src/foo/internal/fooserver.cc, then it contains the order of the header files are as follows:

#include "foo/public/fooserver.h"  //首选的头文件放在第一位#include <sys/types.h>#include <unistd.h>#include #include <vector>#include "base/basictypes.h"#include "base/commandlineflags.h"#include "foo/public/bar.h"

Here's what the implicit dependency is, the hidden dependency: a header file that relies on other header files.

For example:

//A.h中:struct BS bs;...//B.h中:struct BS{....};//在A.c中//这样会报错#include A.h #include B.h//先包含B.h就可以#include B.h#include A.h

This is called "hiding dependencies". If you first include A.h, you can find hidden dependencies, so various specifications require their own header files in the first, you can find hidden dependencies. The workaround is to include B.h in the A.h, not in A.C.

(2) The header file should be included in the project folder name, that is, if you have such a project base, there is a logging.h, then the external include this header file should be written:
#include "Base/logging.h" instead of # include "Logging.h"

What we see here is the hidden purpose behind the principles advocated by the Google C + + programming style guide:

    1. In order to reduce the hidden dependency, the header file and its implementation file match, should first contain its preferences (that is, its corresponding header file).

    2. In addition to preferences, it follows from general to special principles. But I think the order of the Google C + + programming style guide: C standard library, C + + standard library, other library header files, your own project header file missing the first item: OS-level header files, such as the above example sys/ Types.h estimates are not included in the C standard library, but the SDK provided by the Linux operating system. So I think the more accurate statement should be:OS SDK. h, C standard library, standard C + + library, header files for other libraries, and your own project header files .

    3. The reason that the header file is located in the project directory, the role should be the same namespace, is to distinguish the file name accidentally caused.

Reference documents

[1]http://www.cnblogs.com/clever101/archive/2011/08/21/2147892.html
[2]http://www.cnblogs.com/frydsh/p/3466660.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The inclusion order of the header files for the Google C + + programming style guide

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.