pragma once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacob Jensen

    pragma once

    Hi

    I have a problem creating C++ code that is multiplatform compilable. My
    problem is using the "#pragma once" directive which can be used by microsoft
    Visual Studio pre-compiler but which gives warnings when used by gcc for
    example. I would like to use it when compiling with Visual C++ compiler, but
    how can I create a smart macro or something, so that it is truly ignored
    when compiler with gcc. I have tried the following which actually dose not
    serve my wish:

    1-
    #ifdef VISUAL_STUDIO_C OMPILER
    #define MY_ONCE once
    #else
    #define MY_ONCE
    #endif

    and then to use it I simply type:
    #pragma MY_ONCE

    But this still gives a warning when compiled with gcc with the -Wall option
    given.

    2-
    In a pragma_once.h file I type the following
    #ifdef VISUAL_STUDIO_C OMPILER
    #pragma once
    #endif

    And then whenever i need it I include pragma_once.h. The problem with this
    is that it does not work, since pragma once directive is only effective for
    the file it is located in, in this case pragma_once.h

    3-
    I do not have any other ideas but define a macro in some way as below:
    #ifdef VISUAL_STUDIO_C OMPILER
    #define PRAGMA_ONCE "#pragma once"
    #else
    #define PRAGMA_ONCE
    #endif

    but this does not compile, and I found it impossible to compile it with
    similar structures.




  • Guest's Avatar

    #2
    Re: pragma once

    Dont use microsoft specific things

    Use
    -----------------
    #ifndef __MY_HEADER_FIL E_H__
    #define __MY_HEADER_FIL E_H__

    // my code here

    #endif // __MY_HEADER_FIL E_H__
    -----------------
    instead of
    -----------------
    #pragma once

    // my code here
    -----------------


    Comment

    • Jacob Jensen

      #3
      Re: pragma once

      But I want to use the pragma once directive with my microsoft compiler. Is
      there no other option in to your knowledge?

      Thank you again in advance
      Jacob

      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
      news:c4m182$e5v $1@nic.grnet.gr ...[color=blue]
      > Dont use microsoft specific things
      >
      > Use
      > -----------------
      > #ifndef __MY_HEADER_FIL E_H__
      > #define __MY_HEADER_FIL E_H__
      >
      > // my code here
      >
      > #endif // __MY_HEADER_FIL E_H__
      > -----------------
      > instead of
      > -----------------
      > #pragma once
      >
      > // my code here
      > -----------------
      >
      >[/color]


      Comment

      • Jacob Jensen

        #4
        Re: pragma once

        But I want to use the pragma once directive with my microsoft compiler. Is
        there no other option in to your knowledge?

        Thank you again in advance
        Jacob

        "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
        news:c4m182$e5v $1@nic.grnet.gr ...[color=blue]
        > Dont use microsoft specific things
        >
        > Use
        > -----------------
        > #ifndef __MY_HEADER_FIL E_H__
        > #define __MY_HEADER_FIL E_H__
        >
        > // my code here
        >
        > #endif // __MY_HEADER_FIL E_H__
        > -----------------
        > instead of
        > -----------------
        > #pragma once
        >
        > // my code here
        > -----------------
        >
        >[/color]


        Comment

        • Guest's Avatar

          #5
          Re: pragma once

          > But I want to use the pragma once directive with my microsoft compiler. Is[color=blue]
          > there no other option in to your knowledge?[/color]

          in this case try this: (_MSC_VER)
          I think I saw it inside gl.h (OpenGL header)
          UNTESTED
          [color=blue][color=green]
          > > -----------------[/color][/color]
          #ifdef _MSC_VER[color=blue][color=green]
          > > #pragma once[/color][/color]
          #endif[color=blue][color=green]
          > >
          > > // my code here
          > > -----------------
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Heinz Ozwirk

            #6
            Re: pragma once

            "Jacob Jensen" <jacob_news_dkN oSpaaaammmm@yah oo.co.uk> schrieb im Newsbeitrag news:nCBbc.430$ KX.319@news.get 2net.dk...
            : But I want to use the pragma once directive with my microsoft compiler. Is
            : there no other option in to your knowledge?

            If you really want to use #pragma once, do it as Microsoft does:

            #if _MSC_VER > 1000
            #pragma once
            #endif // _MSC_VER > 1000

            HTH
            Heinz

            Comment

            • Guest's Avatar

              #7
              Re: pragma once

              > But I want to use the pragma once directive with my microsoft compiler. Is[color=blue]
              > there no other option in to your knowledge?[/color]

              in this case try this: (_MSC_VER)
              I think I saw it inside gl.h (OpenGL header)
              UNTESTED
              [color=blue][color=green]
              > > -----------------[/color][/color]
              #ifdef _MSC_VER[color=blue][color=green]
              > > #pragma once[/color][/color]
              #endif[color=blue][color=green]
              > >
              > > // my code here
              > > -----------------
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Heinz Ozwirk

                #8
                Re: pragma once

                "Jacob Jensen" <jacob_news_dkN oSpaaaammmm@yah oo.co.uk> schrieb im Newsbeitrag news:nCBbc.430$ KX.319@news.get 2net.dk...
                : But I want to use the pragma once directive with my microsoft compiler. Is
                : there no other option in to your knowledge?

                If you really want to use #pragma once, do it as Microsoft does:

                #if _MSC_VER > 1000
                #pragma once
                #endif // _MSC_VER > 1000

                HTH
                Heinz

                Comment

                • Julie

                  #9
                  Re: pragma once

                  "<- Chameleon ->" wrote:[color=blue]
                  > Use
                  > -----------------
                  > #ifndef __MY_HEADER_FIL E_H__
                  > #define __MY_HEADER_FIL E_H__[/color]

                  Double-underscore identifiers are reserved for implementation purposes. Use
                  something like:

                  #define MY_HEADER_FILE_ H

                  instead.

                  Comment

                  • Julie

                    #10
                    Re: pragma once

                    "<- Chameleon ->" wrote:[color=blue]
                    > Use
                    > -----------------
                    > #ifndef __MY_HEADER_FIL E_H__
                    > #define __MY_HEADER_FIL E_H__[/color]

                    Double-underscore identifiers are reserved for implementation purposes. Use
                    something like:

                    #define MY_HEADER_FILE_ H

                    instead.

                    Comment

                    • Kevin Goodsell

                      #11
                      Re: pragma once

                      <- Chameleon -> wrote:
                      [color=blue]
                      > Dont use microsoft specific things
                      >
                      > Use
                      > -----------------
                      > #ifndef __MY_HEADER_FIL E_H__
                      > #define __MY_HEADER_FIL E_H__[/color]

                      No, don't use this. Not if you want well-defined code.

                      17.4.3.1.2 Global names [lib.global.name s]

                      1 Certain sets of names and function signatures are always reserved to
                      the implementation:

                      --Each name that contains a double underscore __) or begins with an
                      underscore followed by an uppercase letter (_lex.key_) is reserved
                      to the implementation for any use.

                      In short, don't use identifiers that begin with an underscore or contain
                      a sequence of 2 underscores unless you really know what you are doing.

                      -Kevin
                      --
                      My email address is valid, but changes periodically.
                      To contact me please use the address from a recent posting.

                      Comment

                      • Cy Edmunds

                        #12
                        Re: pragma once

                        "Jacob Jensen" <jacob_news_dkN oSpaaaammmm@yah oo.co.uk> wrote in message
                        news:nCBbc.430$ KX.319@news.get 2net.dk...[color=blue]
                        > But I want to use the pragma once directive with my microsoft compiler.[/color]

                        This seems like a very strange requirement. Why?

                        --
                        Cy



                        Comment

                        • Cy Edmunds

                          #13
                          Re: pragma once

                          "Julie" <julie@nospam.c om> wrote in message
                          news:406F02C5.2 83F6763@nospam. com...[color=blue]
                          > "<- Chameleon ->" wrote:[color=green]
                          > > Use
                          > > -----------------
                          > > #ifndef __MY_HEADER_FIL E_H__
                          > > #define __MY_HEADER_FIL E_H__[/color]
                          >
                          > Double-underscore identifiers are reserved for implementation purposes.[/color]
                          Use[color=blue]
                          > something like:
                          >
                          > #define MY_HEADER_FILE_ H
                          >
                          > instead.[/color]

                          Indeed. Even a single leading underscore followed by a capital letter is
                          reserved for implementors. Instead of remembering all these arcane rules,
                          best not to use leading underscores at all.

                          --
                          Cy



                          Comment

                          • Kevin Goodsell

                            #14
                            Re: pragma once

                            <- Chameleon -> wrote:
                            [color=blue]
                            > Dont use microsoft specific things
                            >
                            > Use
                            > -----------------
                            > #ifndef __MY_HEADER_FIL E_H__
                            > #define __MY_HEADER_FIL E_H__[/color]

                            No, don't use this. Not if you want well-defined code.

                            17.4.3.1.2 Global names [lib.global.name s]

                            1 Certain sets of names and function signatures are always reserved to
                            the implementation:

                            --Each name that contains a double underscore __) or begins with an
                            underscore followed by an uppercase letter (_lex.key_) is reserved
                            to the implementation for any use.

                            In short, don't use identifiers that begin with an underscore or contain
                            a sequence of 2 underscores unless you really know what you are doing.

                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Cy Edmunds

                              #15
                              Re: pragma once

                              "Jacob Jensen" <jacob_news_dkN oSpaaaammmm@yah oo.co.uk> wrote in message
                              news:nCBbc.430$ KX.319@news.get 2net.dk...[color=blue]
                              > But I want to use the pragma once directive with my microsoft compiler.[/color]

                              This seems like a very strange requirement. Why?

                              --
                              Cy



                              Comment

                              Working...