Jump to content
  • Advertisement
Sign in to follow this  
Tera_Dragon

To include a file once

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Another newer and standard pattern to use is:
#if !defined(_FILENAME_H_)
#define _FILENAME_H_

// Code here

#endif

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!