Connecting Tech Pros Worldwide Forums | Help | Site Map

#pragma once vs #ifndef on V.S.

Expert
 
Join Date: Sep 2007
Location: VA
Posts: 419
#1: Oct 24 '07
Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef.

From my understanding #pragma is compiler dependent, meaning that it will be handled differently depending on the compiler? #ifndef is not dependent on the compiler.

So from that i take it that its more universal to use #ifndef because its the standard, but if I use #pragma once in visual studios and then create the .exe for the program does it matter whether #ifndef or #pragma once was used?

I guess this could extend into what happens when i build my .exe how are the different files defined. The complier takes the code and translates it into an intermediate language which is easier for the computer to understand right?

Hopefully this question didn't get to mucked up and now i feel like i am rambling.

RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#2: Oct 24 '07

re: #pragma once vs #ifndef on V.S.


Quote:

Originally Posted by Studlyami

Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef.

From my understanding #pragma is compiler dependent, meaning that it will be handled differently depending on the compiler? #ifndef is not dependent on the compiler.

So from that i take it that its more universal to use #ifndef because its the standard, but if I use #pragma once in visual studios and then create the .exe for the program does it matter whether #ifndef or #pragma once was used?

I guess this could extend into what happens when i build my .exe how are the different files defined. The complier takes the code and translates it into an intermediate language which is easier for the computer to understand right?

Hopefully this question didn't get to mucked up and now i feel like i am rambling.

#pragma supports many different directives. #ifndef only detects if something has been previously defined.

http://msdn2.microsoft.com/en-us/lib...05(vs.71).aspx
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#3: Oct 25 '07

re: #pragma once vs #ifndef on V.S.


The #ifndef/#define/#endif is the ANSI standard way of constructing an inclusion guard.

#pragma once is a Microsoft whiz-bang that does the same thing but is not portable. There is no #pragma in ANSI C or C++.

Wherever possible use the ANSI proprocessor directives.
Expert
 
Join Date: Sep 2007
Location: VA
Posts: 419
#4: Oct 25 '07

re: #pragma once vs #ifndef on V.S.


Now after the program is built and i send out the .exe it doesn't matter what was used #pragma once or the #ifndef, #define right? So it would only matter if i sent out my source code and someone tried to built it on a different compiler?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#5: Oct 25 '07

re: #pragma once vs #ifndef on V.S.


Quote:

Originally Posted by weaknessforcats

There is no #pragma in ANSI C or C++.

Hmmm, I thought that the #pragma directive was defined in both ANSI C and C++ with the following conditions.

1. The ANSI C and C++ standards do not define any syntax following the #pragma directive since they are compiler dependent commands.

2. The ANSI C and C++ standards does specify that compilers should ignore unrecognised commands (or at least not produce an error halting compilation).


Apart from that I agree, there is little point using the MS specific way when you can use a method supported by everyone.

Even in Visual Studio generated projects the standard method is used in headers. If an MS compiler is detected then it additionally uses the "#pragma once" directive.

In a very large project the advantage of using #pragma once is that the header never even gets opened again during a compile, this can speed up the compile time of the project.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#6: Oct 25 '07

re: #pragma once vs #ifndef on V.S.


I stand corrected. I looked in K&R The C Programming Language 1978 and there is no #pragma.

Based on your comment, I looked in K&R The ANSI C Progammiong Language 1988 and there it is.

I really need to throw that old K&R away.
Expert
 
Join Date: Sep 2007
Location: VA
Posts: 419
#7: Oct 25 '07

re: #pragma once vs #ifndef on V.S.


Thanks guys for the information.
Reply