Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pragma once

pragma once

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sarvan AL
    wrote on last edited by
    #1

    Hi All, When we include a class, the wizard creates two files (.h and .cpp) in the class's name. The .cpp file starts with like this:

    #ifndef _FILE1_H
    #define _FILE1_H
    #include "File1.h"
    #endif

    Hope this is a way to avoid including the file (.h) more than once in the application. How this .cpp file is getting related with its corresponding .h file? Because, we include only .h file. Kindly let me know that how can compare this with "# pragma once" and more about "pragma". Thanks in advance, Sarvan AL

    *** Live Life To Its Fullest ***

    R 1 Reply Last reply
    0
    • S Sarvan AL

      Hi All, When we include a class, the wizard creates two files (.h and .cpp) in the class's name. The .cpp file starts with like this:

      #ifndef _FILE1_H
      #define _FILE1_H
      #include "File1.h"
      #endif

      Hope this is a way to avoid including the file (.h) more than once in the application. How this .cpp file is getting related with its corresponding .h file? Because, we include only .h file. Kindly let me know that how can compare this with "# pragma once" and more about "pragma". Thanks in advance, Sarvan AL

      *** Live Life To Its Fullest ***

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      Sarvan AL wrote:

      Hope this is a way to avoid including the file (.h) more than once in the application

      Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:

      #ifndef _FILE1_H
      #define _FILE1_H

      .. here the declarations

      #endif

      In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.

      ~RaGE();

      I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

      S S 2 Replies Last reply
      0
      • R Rage

        Sarvan AL wrote:

        Hope this is a way to avoid including the file (.h) more than once in the application

        Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:

        #ifndef _FILE1_H
        #define _FILE1_H

        .. here the declarations

        #endif

        In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.

        ~RaGE();

        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

        S Offline
        S Offline
        Sarvan AL
        wrote on last edited by
        #3

        Hi Rage, Thank you for your detailed explanation. Sarvan AL

        *** Live Lift To Its Fullest ***

        1 Reply Last reply
        0
        • R Rage

          Sarvan AL wrote:

          Hope this is a way to avoid including the file (.h) more than once in the application

          Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:

          #ifndef _FILE1_H
          #define _FILE1_H

          .. here the declarations

          #endif

          In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.

          ~RaGE();

          I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

          S Offline
          S Offline
          sunit5
          wrote on last edited by
          #4

          Adding to Rage explanation on #pragma once, the header file is scanned only once(internally it is maintained in a table)where as

          Rage wrote:

          #ifndef _FILE1_H #define _FILE1_H .. here the declarations #endif

          if the header file is included more than once in a .cpp file,it is scanned that no times -- modified at 8:04 Wednesday 16th August, 2006 -- modified at 8:05 Wednesday 16th August, 2006

          never say die

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups