#ifndef vs #pragma once

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Mloren
Posts: 114
Joined: Mon Aug 07, 2006 2:30 am
Location: Australia
Contact:

#ifndef vs #pragma once

Post by Mloren »

I have been wodnering the difference between using:

#ifndef _SOMETHING_
#define _SOMETHING_

#endif

and the newer:

#pragma once

I read up on it on wikipedia and it seems to indicate that #pragma once is better for performance reasons. Visual C++ automatically inserts #pragma ocne if you create a new class which would indicate that microsoft agrees.

Yet i see lots of people still using the #ifndef method. is this just out of habbit? or is there some reason its better?

Which do you use? and why?
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

#if !defined AreaManager_H_
#define AreaManager_H_
...
#endif

I use that because I know it works and I haven't tried anything else. ;)

I use Dev-Cpp 4.9.9.2
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

#pragma once is less portable. That's enough reason for me.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Same reason as vittek. #pragma is not portable and because i'm currently not working with VS it would therefore simply not work for me.

As for faster, hm, it might compile somewhat faster. Not sure if you would ever notice that. Certainly no speed difference at runtime.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, the compiler can skip the whole file when reaching the pragma position, while the ifndef solution requires the compiler to parse the whole file - usually finding just the matching endif right at the end of the file. However, you could (but should not :wink: ) add something behind the endif which would be compiled. And since the compiler (in fact the preprocessor) has to parse the code to some extent it might be quite slow, in addition to the I/O overhead.
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Post by Klasker »

I always hated the #pragma directive. Good old #ifndef always works.

Reminds me of a great strip a friend sent me: http://www.hackles.org/cgi-bin/archives.pl?request=52
nullterm
Posts: 14
Joined: Tue Feb 27, 2007 12:36 pm

Post by nullterm »

Always been a #ifndef user. But now that you mention it #pragma once might be faster. For hobby stuff, I only ever use VS for Windows so might as well gradually switch.

On a side note, I've made it a habit to use

Code: Select all

#pragma comment( lib, "mylib.lib" )
Instead of adding files to the .vcproj. Makes life easier when sharing code around between projects.
Post Reply