C++ Header Guards
by Harley Laue · in Torque 3D Beginner · 09/21/2012 (10:41 am) · 1 replies
Ok, so since the release of the Torque 3D as open source software, I've been checking it out and trying to compile it Linux/GCC (Clang may come later.) One thing I've noticed that's struck me as odd is that headers have the standard indef/define guard, but headers that include that header also check for that guard before including the header. So, as an example:
platform/types.h has:
Then core/crc.h has:
Now, the only possible thing this does is perhaps stops an additional disk read of the header file (and actually, on Linux, that additional read may not even happen to the disk, but to ram instead...) The preprocessor is obviously going to get to that file again see that _TORQUE_TYPES_H_ is already defined and skip the rest of the file. So, was this an attempt at making the build go faster because it doesn't seem to me to serve any other functionality.
platform/types.h has:
#ifndef _TORQUE_TYPES_H_ #define _TORQUE_TYPES_H_ // the rest of the types header #endif // _TORQUE_TYPES_H_
Then core/crc.h has:
#ifndef _CRC_H_ #define _CRC_H_ #ifndef _TORQUE_TYPES_H_ #include "platform/types.h" #endif // Rest of crc header #endif // _CRC_H_
Now, the only possible thing this does is perhaps stops an additional disk read of the header file (and actually, on Linux, that additional read may not even happen to the disk, but to ram instead...) The preprocessor is obviously going to get to that file again see that _TORQUE_TYPES_H_ is already defined and skip the rest of the file. So, was this an attempt at making the build go faster because it doesn't seem to me to serve any other functionality.
Associate David Wyand
Gnometech Inc.
You've figured out the exact reason. Torque 3D has a very long history, back to Tribes 2. Back then you wanted to minimized file access (and memory usage) to improve your compile times as it did make a difference.
Of course, today it is not as much of a concern given the hardware power we have. Although by saying that, I've instantly made all my hardware developer friends mad... ;)
- Dave