C / C ++包含头文件的顺序 [英] C/C++ include header file order

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

问题描述

应指定文件的顺序,即,为什么要在一个头之前包含一个头?

What order should include files be specified, i.e. what are the reasons for including one header before another?

例如,系统文件,STL和Boost是在本地包含文件之前还是之后?

For example, do the system files, STL, and Boost go before or after the local include files?

推荐答案

我认为没有推荐的顺序,只要可以编译即可!令人讨厌的是,当某些标头要求首先包含其他标头时...这是标头本身的问题,而不是包含顺序的问题。

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.

我个人的喜好是从本地到全局,每个子节都按字母顺序排列,即:

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


  1. h与此cpp文件相对应的文件(如果有)

  2. 同一组件的头,

  3. 其他组件的头,

  4. 系统头。

  1. h file corresponding to this cpp file (if applicable)
  2. headers from the same component,
  3. headers from other components,
  4. system headers.

我对1.的理由是,它应该证明每个标头(有一个cpp)可以是 #include d没有先决条件(terminus technicus:标头是独立的)。其余的似乎只是从那里顺理成章地流了起来。

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

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

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