#pragma once vs #ifndef on V.S.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #pragma once vs #ifndef on V.S.

    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
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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.

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        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?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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.

            Comment

            • Studlyami
              Recognized Expert Contributor
              • Sep 2007
              • 464

              #7
              Thanks guys for the information.

              Comment

              • solita
                New Member
                • Jun 2009
                • 17

                #8
                #pragma once is compiler dependent.#ifnd ef has no such problem

                Comment

                Working...