在#ifndef的中包含#includes - 添加任何值? [英] Wrapping #includes in #ifndef's - adds any value?

查看:465
本文介绍了在#ifndef的中包含#includes - 添加任何值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已继承 C / C ++代码库,在许多.cpp文件中, #include 指令包装在#ifndef的标题内部单个包括 #define



例如

  #ifndef _INC_WINDOWS 
#include< windows.h>
#endif

和windows.h看起来像

  #ifndef _INC_WINDOWS 
#define _INC_WINDOWS
...头文件stuff ....
#endif // _INC_WINDOWS



我假设这样做是为了加快代码的编译/预处理。



我认为这是一个丑陋的和过早的优化,但是因为该项目有一个5分钟的构建时间从干净,我不想让事情变得更糟。



所以练习添加任何值或加快速度吗?是否可以清理它们?



更新:编译器是MSVC(VS2005),平台是Win32 / WinCE

#pragma once 和/或头部包含保护检测优化,



在这些编译器中,包括MSVC和GCC,这个优化 是无意义的,它应该是头文件负责处理多个包含。然而,这可能是编译器的优化,其中#include是非常低效的。代码在病理上是可移植的,< windows.h> 不是指知名的Win32头文件,而是指一些用户定义的同名头文件?



这也可能是头文件没有多个包含守卫,这个检查实际上是必要的。在这种情况下,我建议更改标题。头部的整个点是作为关于该地方的复制和粘贴代码的替代:它不应该采用三行来包括头部。



编辑: / p>

因为你说你只关心MSVC,我会:




  • do一个大规模编辑,时间的构建只是为了确保以前的程序员不知道我不知道的东西。也许添加 #pragma once 如果它有帮助。

  • 忽略它,但不要为新文件或新的 #include 已添加到旧文件。



取决于我是否有更重要的事情需要担心。这是一个经典的星期五下午的工作,我不会花费潜在的生产时间; - )


I have inherited C/C++ code base, and in a number of .cpp files the #include directives are wrapped in #ifndef's with the headers internal single include #define.

for example

#ifndef _INC_WINDOWS
#include <windows.h>
#endif

and windows.h looks like

#ifndef _INC_WINDOWS
#define _INC_WINDOWS
...header file stuff....
#endif // _INC_WINDOWS

I assume this was done to speed up the compile/preprocess of the code.

I think it's ugly and a premature optimisation, but as the project has a 5 minute build time from clean, I don't want to makes things worse.

So does the practice add any value or speed things up lots? Is it OK to clean them up?

Update: compiler is MSVC (VS2005) and platform is Win32/WinCE

解决方案

It's worth knowing that some implementations have #pragma once and/or a header-include-guard detection optimisation, and that in both cases the preprocessor will automatically skip opening, reading, or processing a header file which it has included before.

So on those compilers, including MSVC and GCC, this "optimisation" is pointless, and it should be the header files responsibility to handle multiple inclusion. However, it's possible that this is an optimisation for compilers where #include is very inefficient. Is the code pathologically portable, and <windows.h> refers not to the well-known Win32 header file, but to some user-defined header file of the same name?

It's also possible that the header files don't have multiple-include guards, and that this check is actually essential. In which case I'd suggest changing the headers. The whole point of headers is as a substitute for copy-and-pasting code about the place: it shouldn't take three lines to include a header.

Edit:

Since you say you only care about MSVC, I would either:

  • do a mass edit, time the build just to make sure the previous programmer doesn't know something I don't. Maybe add #pragma once if it helps. Use precompiled headers if all this really is slowing things down.
  • Ignore it, but don't use the guards for new files or for new #includes added to old files.

Depending on whether I had more important things to worry about. This is a classic Friday-afternoon job, I wouldn't spend potentially-productive time on it ;-)

这篇关于在#ifndef的中包含#includes - 添加任何值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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