#pragma once vs Inclusion Guards
Just a quick question regarding the difference between :
#pragma once
and
#ifndef NODE_H
#define NODE_H
...
#endif //NODE_H
as protection against multiple inlusion... or is the question wrong? Are these used for different purposes? I use the second method everywhere but method 2 seems much simpler...Is there a downside?
Thanks
Chris
Chris Brodie
As long as you''re always going to be using MSVC, they''re the same. #pragma once isn''t standard, as in, not every compiler will understand it.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
Yes there is.
With #ifdef, the compiler must open the file, skip the definition, and close the file.
With #pragma, the compiler internally do some housekeeping and skip opening the file a second time. (Of course, this is compiler dependant)
So the second should produce slightly faster compilation times on larger projects.
With #ifdef, the compiler must open the file, skip the definition, and close the file.
With #pragma, the compiler internally do some housekeeping and skip opening the file a second time. (Of course, this is compiler dependant)
So the second should produce slightly faster compilation times on larger projects.
Resist MS-"standards". Need I say more?
cu,
Prefect
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
Sanity is the trademark of a weak mind.
cu,
Prefect
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
Sanity is the trademark of a weak mind.
Widelands - laid back, free software strategy
Thanks... I guess then this applies to all #pragma''s? Is this not a part of the standard or is it a recent addition?
Chris Brodie
Is there any way to specify in the code to only compile certain lines if you are in MSVC or any other compiler? So like if i said something along the lines of:
#ifdef _MSVC
#pragma once
#else
other stuff here....
#endif
Is there a way to do something like that? So that way, if im in MSVC, it would use the pragma once and if its not, then use the other way to not include the same header millions of times.
-SirKnight
#ifdef _MSVC
#pragma once
#else
other stuff here....
#endif
Is there a way to do something like that? So that way, if im in MSVC, it would use the pragma once and if its not, then use the other way to not include the same header millions of times.
-SirKnight
I don''t think that would help ![](smile.gif)
The file would still have to be opened and closed to see the ifdefs, thus eliminating any speed increase caused by using #pragma.
~~~~~~~~~~
Martee
![](smile.gif)
The file would still have to be opened and closed to see the ifdefs, thus eliminating any speed increase caused by using #pragma.
~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Martee: I think that _would_ work actually, because in msvc, the compiler would open the file the first time, see the #pragma once, and then it wouldn''t have to open the file again. Other compilers would have to open it every time it was included though.
Scott
Scott
Oops. I stand corrected
.
~~~~~~~~~~
Martee
![](smile.gif)
~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
If your header files are huge, #pragma once is a lot faster than the #ifndef approach.
That''s because if your header file is 500K large (just look at the Win32 headers), everytime the file is included, the compiler will need to read 500K from the #ifndef just to find the #endif. This IS a major waste of time, especially if the file is included many times.
While #pragma once isn''t standard, it is supported by many compilers, MSVC, lcc-win32, gcc, probably others that I haven''t tried. My opinion? Use it
That''s because if your header file is 500K large (just look at the Win32 headers), everytime the file is included, the compiler will need to read 500K from the #ifndef just to find the #endif. This IS a major waste of time, especially if the file is included many times.
While #pragma once isn''t standard, it is supported by many compilers, MSVC, lcc-win32, gcc, probably others that I haven''t tried. My opinion? Use it
![](smile.gif)
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement