To include a file once

Started by
15 comments, last by Zahlman 18 years, 11 months ago
To make sure a header is only included once I've always used #ifndef _FILENAME_H #define _FILENAME_H ... code ... #endif Though I have also seen #pragma once used. What's the difference between the two? Is one way out dated now? Or is it just preference?
____________________________________________________________Programmers Resource Central
Advertisement
As far as i'm aware the #ifndef etc is C/C++, whereas #pragma is a Microsoft Visual C++ directive that is not in the actual language. Both do the same thing but porting code to Linux with #pragma in won't work.

ace
#pragma once

is only for MSVC (i think) and is not portable.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
I had a feeling that it was that as I saw it in a simple program template from MSVC.
____________________________________________________________Programmers Resource Central
Additionally, #pragma once improves compilation peformance under MSVC (because the compiler can make assumptions about the way it handles the header).

Of course, GCC does the same optimisation when using normal include guards (#ifndef/#define), although MSVC dosn't.
Quote:Original post by Andrew Russell
Additionally, #pragma once improves compilation peformance under MSVC (because the compiler can make assumptions about the way it handles the header).


You obviously know this, but it isn't that the compiler 'can' make assumptions it's because it 'chooses to' make the assumptions. It could very well make those assumptions without #pragma once being used.

Just being picky
(c:
Quote:Original post by ace_lovegrove
As far as i'm aware the #ifndef etc is C/C++, whereas #pragma is a Microsoft Visual C++ directive that is not in the actual language. Both do the same thing but porting code to Linux with #pragma in won't work.
It works if you're compiling with GCC (although earlier versions did emit a warning message) or ICC.
Another newer and standard pattern to use is:
#if !defined(_FILENAME_H_)#define _FILENAME_H_// Code here#endif
Rob Loach [Website] [Projects] [Contact]
Isn't underscore then uppercase reserved for the compiler's use?
At the company I work, the standard is to use the company's abbreviated name the the filename, like CO_FILENAME
For personaly projects, I use a project prefix, the filename, then 'h':
LIB_Threading_H
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Rob Loach
Another newer and standard pattern to use is:
#if !defined(_FILENAME_H_)#define _FILENAME_H_// Code here#endif


Why is that a standard pattern as opposed to:
#ifndef _FILENAME_H_#define _FILENAME_H_// Code here#endif

Am I missing something?

This topic is closed to new replies.

Advertisement