Advertisement

#pragma once vs Inclusion Guards

Started by March 25, 2001 10:07 PM
14 comments, last by gimp 23 years, 10 months ago
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
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Well, if your header files are huge and you''re desperate, you could always create a small header file like:

#ifndef _THISXYZ_INCLUDED_
#define _THISXYZ_INCLUDED_

#include "my_huge_header_file.h"

#endif

That way you''d at least be rid of the scanning through the huge file more than once.
Advertisement
I think it should also be mentioned that you don''t need to do such things as:

#ifdef _MSVC
#pragma once
#endif

Just leave it as this:

#pragma once
#ifndef __THISFILE_H__
#define __THISFILE_H__

...

#endif

The #pragma won''t choke a compiler that doesn''t support it. Thats one of the things about #pragmas. They''re there to use specific features, yet if the compiler doesn''t understand, then it just ignores it (some compiler will give a warning that it didn''t understand it, but code will still compile).


--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Ah, goody goody.

-SirKnight
quote:
Original post by Promiscuous Robot

The #pragma won''t choke a compiler that doesn''t support it. Thats one of the things about #pragmas. They''re there to use specific features, yet if the compiler doesn''t understand, then it just ignores it (some compiler will give a warning that it didn''t understand it, but code will still compile).


But what if 2 compilers interpret the same directive in different ways?

Then you''re boned
You need those #ifdef _SOLARIS type things (commonly used one to fix sun''s broken realloc...)

Are not ALL #pragma''s, by definition, compiler dependent?

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement