C++头文件的包含顺序研究

               

 

作者:朱金灿

来源:http://blog.csdn.net/clever101

 

一.《Google C++ 编程风格指南》里的观点

 

     公司在推行编码规范,领导提议基本上使用《Google 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:

 

#include "foo/public/fooserver.h"  // Preferred location.#include <sys/types.h>#include <unistd.h> #include <hash_map>#include <vector> #include "base/basictypes.h"#include"base/commandlineflags.h"#include "foo/public/bar.h" 


     在这里我谈一下我对上面的理解(如不当,还请诸位同学指正):

1.      为了加强可读性和避免隐含依赖&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值