C / C ++包括文件顺序/最佳实践 [英] C/C++ include file order/best practices

查看:139
本文介绍了C / C ++包括文件顺序/最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有推荐的顺序包含文件应指定?

Is there recommended order in which include files should be specified?

例如,系统文件,STL,升压,去之前的后局部有文件吗?
是否有特别的原因,选择一种或另一种?我假设包括指定适当的依赖关系的文件。

For example, the system files, STL, Boost, go before of after local include files? Is there a particular reason to choose one or another? I am assuming include files have proper dependencies specified.

推荐答案

我不认为有一个建议顺序,只要它编译!当一些头需要其他的头被首次列入什么是可气的是......这是用自己的头一个问题,与包括的顺序。

I don't think there's a recommended order, as long as it compiles! What's annoying is when some headers require other headers to be included first... That's a problem with the headers themselves, not with the order of includes.

我个人的preference是从地方去全球性的,按字母顺序排列的每个小节,即:

My personal preference is to go from local to global, each subsection in alphabetical order, i.e.:


  1. 对应于该CPP文件.h文件(如适用)

  2. 从相同的组件头,

  3. 从其他组件标题,

  4. 系统头。

我对1的理由是,它应该证明,每头(对此有一个CPP)可的#include D没有prerequisites。剩下的似乎只是从那里流逻辑

My rationale for 1. is that it should prove that each header (for which there is a cpp) can be #included without prerequisites. And the rest just seems to flow logically from there.

这篇关于C / C ++包括文件顺序/最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆