C++头文件包含顺序

《Google C++ 编程风格指南》对于头文件的包含顺序是这样的:

Names and Order of Includes

link Use standard order for readability and to avoid hidden dependencies:C library, C++ library, other libraries' .h, your project's .h.

All of a project's header files should belisted as descendants of the project's source directory without use of UNIXdirectory shortcuts . (the current directory) or .. (the parent directory). Forexample, google-awesome-project/src/base/logging.h should be included as

#include "base/logging.h"

In dir/foo.cc or dir/foo_test.cc, whosemain purpose is to implement or test the stuff in dir2/foo2.h, order yourincludes as follows:

dir2/foo2.h (preferred location — seedetails below).

C system files.

C++ system files.

Other libraries' .h files.

Your project's .h files.

The preferred ordering reduces hiddendependencies. We want every header file to be compilable on its own. Theeasiest way to achieve this is to make sure that every one of them is the first.h file #included in some .cc.

dir/foo.cc and dir2/foo2.h are often in thesame directory (e.g. base/basictypes_test.cc and base/basictypes.h), but can bein different directories too.

Within each section it is nice to order theincludes alphabetically.

 

For example, the includes ingoogle-awesome-project/src/foo/internal/fooserver.cc might look like this:

[cpp] view plaincopy

  1. <span style="font-size:16px;">  
  2. #include "foo/public/fooserver.h"  // Preferred location.  
  3.   
  4. #include <sys/types.h>  
  5.   
  6. #include <unistd.h>  
  7.   
  8.    
  9.   
  10. #include <hash_map>  
  11.   
  12. #include <vector>  
  13.   
  14.    
  15.   
  16. #include "base/basictypes.h"  
  17.   
  18. #include"base/commandlineflags.h"  
  19.   
  20. #include "foo/public/bar.h"  
  21.   
  22.  </span>  

头文件包含顺序可归纳如下:

1. 为了加强可读性和避免隐含依赖,应使用下面的顺序:C标准库、C++标准库、其它库的头文件、你自己工程的头文件。不过这里最先包含的是首选的头文件,即例如a.cpp文件中应该优先包含a.h。首选的头文件是为了减少隐藏依赖,同时确保头文件和实现文件是匹配的。具体的例子是:假如你有一个cc文件(linux平台的cpp文件后缀为cc)是google-awesome-project/src/foo/internal/fooserver.cc,那么它所包含的头文件的顺序如下:

[cpp] view plaincopy

  1. <span style="font-size:16px;">#include "foo/public/fooserver.h"  // Preferred location.  
  2.   
  3. #include <sys/types.h>  
  4. #include <unistd.h>  
  5.   
  6. #include <hash_map>  
  7. #include <vector>  
  8.   
  9. #include "base/basictypes.h"  
  10. #include "base/commandlineflags.h"  
  11. #include "foo/public/bar.h"  
  12.   
  13. </span>  

2. 在包含头文件时应该加上头文件所在工程的文件夹名,即假如你有这样一个工程base,里面有一个logging.h,那么外部包含这个头文件应该这样写:

#include "base/logging.h",而不是#include "logging.h"

 

我们看到的是这里《Google C++ 编程风格指南》倡导的原则背后隐藏的目的是:

1. 为了减少隐藏依赖,同时头文件和其实现文件匹配,应该先包含其首选项(即其对应的头文件)。

2. 除了首选项外,遵循的是从一般到特殊的原则。不过我觉得《Google C++ 编程风格指南》的顺序:C标准库、C++标准库、其它库的头文件、你自己工程的头文件中漏了最前面的一项:操作系统级别的头文件,比如上面的例子sys/types.h估计不能归入C标准库,而是Linux操作系统提供的SDK吧。因此我觉得更准确的说法应该是:OS SDK .h , C标准库、C++标准库、其它库的头文件、你自己工程的头文件。

3.之所以要将头文件所在的工程目录列出,作用应该是命名空间是一样的,就是为了区分不小心造成的文件重名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值