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_COMPILER
#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_COMPILER
#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_COMPILER
#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. | | | | re: pragma once
Dont use microsoft specific things
Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__
// my code here
#endif // __MY_HEADER_FILE_H__
-----------------
instead of
-----------------
#pragma once
// my code here
----------------- | | | | 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@hotmail.NOSPAM.com> wrote in message
news:c4m182$e5v$1@nic.grnet.gr...[color=blue]
> Dont use microsoft specific things
>
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__
>
> // my code here
>
> #endif // __MY_HEADER_FILE_H__
> -----------------
> instead of
> -----------------
> #pragma once
>
> // my code here
> -----------------
>
>[/color] | | | | 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@hotmail.NOSPAM.com> wrote in message
news:c4m182$e5v$1@nic.grnet.gr...[color=blue]
> Dont use microsoft specific things
>
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__
>
> // my code here
>
> #endif // __MY_HEADER_FILE_H__
> -----------------
> instead of
> -----------------
> #pragma once
>
> // my code here
> -----------------
>
>[/color] | | | | 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] | | | | re: pragma once
"Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> schrieb im Newsbeitrag news:nCBbc.430$KX.319@news.get2net.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 | | | | 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] | | | | re: pragma once
"Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> schrieb im Newsbeitrag news:nCBbc.430$KX.319@news.get2net.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 | | | | re: pragma once
"<- Chameleon ->" wrote:[color=blue]
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__[/color]
Double-underscore identifiers are reserved for implementation purposes. Use
something like:
#define MY_HEADER_FILE_H
instead. | | | | re: pragma once
"<- Chameleon ->" wrote:[color=blue]
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__[/color]
Double-underscore identifiers are reserved for implementation purposes. Use
something like:
#define MY_HEADER_FILE_H
instead. | | | | re: pragma once
<- Chameleon -> wrote:
[color=blue]
> Dont use microsoft specific things
>
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__[/color]
No, don't use this. Not if you want well-defined code.
17.4.3.1.2 Global names [lib.global.names]
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. | | | | re: pragma once
"Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
news:nCBbc.430$KX.319@news.get2net.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 http://home.rochester.rr.com/cyhome/ | | | | re: pragma once
"Julie" <julie@nospam.com> wrote in message
news:406F02C5.283F6763@nospam.com...[color=blue]
> "<- Chameleon ->" wrote:[color=green]
> > Use
> > -----------------
> > #ifndef __MY_HEADER_FILE_H__
> > #define __MY_HEADER_FILE_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 http://home.rochester.rr.com/cyhome/ | | | | re: pragma once
<- Chameleon -> wrote:
[color=blue]
> Dont use microsoft specific things
>
> Use
> -----------------
> #ifndef __MY_HEADER_FILE_H__
> #define __MY_HEADER_FILE_H__[/color]
No, don't use this. Not if you want well-defined code.
17.4.3.1.2 Global names [lib.global.names]
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. | | | | re: pragma once
"Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
news:nCBbc.430$KX.319@news.get2net.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 http://home.rochester.rr.com/cyhome/ | | | | re: pragma once
"Julie" <julie@nospam.com> wrote in message
news:406F02C5.283F6763@nospam.com...[color=blue]
> "<- Chameleon ->" wrote:[color=green]
> > Use
> > -----------------
> > #ifndef __MY_HEADER_FILE_H__
> > #define __MY_HEADER_FILE_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 http://home.rochester.rr.com/cyhome/ | | | | re: pragma once
For compiler efficiency reasons, that is all. It would be a very nice thing
to have for me, and therefore I would like to find a good solution to my
"problem".
"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
news:CkEbc.10104$LW2.9792@twister.nyroc.rr.com...[color=blue]
> "Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
> news:nCBbc.430$KX.319@news.get2net.dk...[color=green]
> > But I want to use the pragma once directive with my microsoft compiler.[/color]
>
> This seems like a very strange requirement. Why?
>
> --
> Cy
> http://home.rochester.rr.com/cyhome/
>
>[/color] | | | | re: pragma once
Jacob Jensen wrote:[color=blue]
>
> For compiler efficiency reasons, that is all. It would be a very nice thing
> to have for me, and therefore I would like to find a good solution to my
> "problem".
>
> "Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
> news:CkEbc.10104$LW2.9792@twister.nyroc.rr.com...[color=green]
> > "Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
> > news:nCBbc.430$KX.319@news.get2net.dk...[color=darkred]
> > > But I want to use the pragma once directive with my microsoft compiler.[/color]
> >
> > This seems like a very strange requirement. Why?
> >
> > --
> > Cy
> > http://home.rochester.rr.com/cyhome/
> >
> >[/color][/color]
If you are after efficiency (compilation speed?), look into precompiled
headers. | | | | re: pragma once
For compiler efficiency reasons, that is all. It would be a very nice thing
to have for me, and therefore I would like to find a good solution to my
"problem".
"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
news:CkEbc.10104$LW2.9792@twister.nyroc.rr.com...[color=blue]
> "Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
> news:nCBbc.430$KX.319@news.get2net.dk...[color=green]
> > But I want to use the pragma once directive with my microsoft compiler.[/color]
>
> This seems like a very strange requirement. Why?
>
> --
> Cy
> http://home.rochester.rr.com/cyhome/
>
>[/color] | | | | re: pragma once
Jacob Jensen wrote:[color=blue]
>
> For compiler efficiency reasons, that is all. It would be a very nice thing
> to have for me, and therefore I would like to find a good solution to my
> "problem".
>
> "Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
> news:CkEbc.10104$LW2.9792@twister.nyroc.rr.com...[color=green]
> > "Jacob Jensen" <jacob_news_dkNoSpaaaammmm@yahoo.co.uk> wrote in message
> > news:nCBbc.430$KX.319@news.get2net.dk...[color=darkred]
> > > But I want to use the pragma once directive with my microsoft compiler.[/color]
> >
> > This seems like a very strange requirement. Why?
> >
> > --
> > Cy
> > http://home.rochester.rr.com/cyhome/
> >
> >[/color][/color]
If you are after efficiency (compilation speed?), look into precompiled
headers. | | | | re: pragma once
Jacob Jensen wrote:
[color=blue]
> But I want to use the pragma once directive with my microsoft compiler.[/color]
Why, when there is a perfectly good, cross-platform, widely-used
convention (i.e. include guards) already out there?
--
Mike Smith | | | | re: pragma once
Jacob Jensen wrote:
[color=blue]
> But I want to use the pragma once directive with my microsoft compiler.[/color]
Why, when there is a perfectly good, cross-platform, widely-used
convention (i.e. include guards) already out there?
--
Mike Smith | | | | re: pragma once
Jacob Jensen wrote:
[color=blue]
> For compiler efficiency reasons, that is all. It would be a very nice thing
> to have for me, and therefore I would like to find a good solution to my
> "problem".[/color]
I've looked through all the postings and I'm not clear either what the
problem is or what pragma once does (and, therefore, how it is supposed
to help).
In general pragmas are completely non-portable. Avoid them if at all
possible. | | | | re: pragma once
Jacob Jensen wrote:
[color=blue]
> For compiler efficiency reasons, that is all. It would be a very nice thing
> to have for me, and therefore I would like to find a good solution to my
> "problem".[/color]
I've looked through all the postings and I'm not clear either what the
problem is or what pragma once does (and, therefore, how it is supposed
to help).
In general pragmas are completely non-portable. Avoid them if at all
possible. | | | | re: pragma once
Bill Seurer wrote:
[color=blue]
> Jacob Jensen wrote:
>[color=green]
>> For compiler efficiency reasons, that is all. It would be a very nice
>> thing
>> to have for me, and therefore I would like to find a good solution to my
>> "problem".[/color]
>
>
> I've looked through all the postings and I'm not clear either what the
> problem is or what pragma once does (and, therefore, how it is supposed
> to help).[/color]
#pragma once does more or less exactly what include guards do; i.e.
prevent a header form being included more than once for any given
translation unit. There is no reason whatsoever (except for somewhat
less typing) to use #pragma once instead of include guards.
--
Mike Smith | | | | re: pragma once
Bill Seurer wrote:
[color=blue]
> Jacob Jensen wrote:
>[color=green]
>> For compiler efficiency reasons, that is all. It would be a very nice
>> thing
>> to have for me, and therefore I would like to find a good solution to my
>> "problem".[/color]
>
>
> I've looked through all the postings and I'm not clear either what the
> problem is or what pragma once does (and, therefore, how it is supposed
> to help).[/color]
#pragma once does more or less exactly what include guards do; i.e.
prevent a header form being included more than once for any given
translation unit. There is no reason whatsoever (except for somewhat
less typing) to use #pragma once instead of include guards.
--
Mike Smith | | | | re: pragma once
Mike Smith wrote:[color=blue]
> #pragma once does more or less exactly what include guards do; i.e.
> prevent a header form being included more than once for any given
> translation unit. There is no reason whatsoever (except for somewhat
> less typing) to use #pragma once instead of include guards.[/color]
I'd suspect that #pragma once would be more efficient during subsequent
inclusions as the pre-processor could terminate the parse and close the file at
that point.
With an include guard, the entire file must still be parsed.
As I previously mentioned, the best solution for inclusion performance is using
pre-compiled headers.
However, both (#pragma once and pch) are compiler-specific issues, and can be
better answered in a forum dealing with the specifics of the OPs compiler.
The only platform-neutral option is include guards. | | | | re: pragma once
Mike Smith wrote:[color=blue]
> #pragma once does more or less exactly what include guards do; i.e.
> prevent a header form being included more than once for any given
> translation unit. There is no reason whatsoever (except for somewhat
> less typing) to use #pragma once instead of include guards.[/color]
I'd suspect that #pragma once would be more efficient during subsequent
inclusions as the pre-processor could terminate the parse and close the file at
that point.
With an include guard, the entire file must still be parsed.
As I previously mentioned, the best solution for inclusion performance is using
pre-compiled headers.
However, both (#pragma once and pch) are compiler-specific issues, and can be
better answered in a forum dealing with the specifics of the OPs compiler.
The only platform-neutral option is include guards. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|