频道栏目
首页 > 资讯 > Vc > 正文

VS2015--win32工程配置的一些想法之Google Code Style中头文件的顺序

16-02-18        来源:[db:作者]  
收藏   我要投稿

工程大了,有很多的头文件,也要引用很多的库文件。
从我们学习C++写hello world的那一刻起,就知道要包含一些系统文件。

那么顺序如何呢?
在review的时候,感觉自己写的东西就是一坨屎。

看看Google code style中是如何描述include文件顺序的:

Names and Order of Includes
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 be listed as descendants of the project’s source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For example, google-awesome-project/src/base/logging.h should be included as

#include "base/logging.h"

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

dir2/foo2.h (preferred location — see details below).
C system files.
C++ system files.
Other libraries’ .h files.
Your project’s .h files.

The preferred ordering reduces hidden dependencies. We want every header file to be compilable on its own. The easiest 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 the same directory (e.g. base/basictypes_test.cc and base/basictypes.h), but can be in different directories too.

Within each section it is nice to order the includes alphabetically.

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

#include "foo/public/fooserver.h"  // Preferred location.

#include 
#include 

#include 
#include 

#include "base/basictypes.h"
#include "base/commandlineflags.h"
#include "foo/public/bar.h"

这用通俗的讲,你写了一个a.h和a.cc,而在.cc文件中需要用到一些通用的东西位于base文件夹下的base.h中。那么在a.cc文件中,你就应该这样的顺序包含:

#include a.h

#include  

#include 

#include "base/base.h"
相关TAG标签
上一篇:C语言typedef struct详解
下一篇:C语言之linux内核--BCD码转二进制与二进制转BCD码
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站