By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,885 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,885 IT Pros & Developers. It's quick & easy.

pragma once

P: n/a
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.


Jul 22 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
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
-----------------
Jul 22 '05 #2

P: n/a
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 ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c4**********@nic.grnet.gr...
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
-----------------

Jul 22 '05 #3

P: n/a
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 ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c4**********@nic.grnet.gr...
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
-----------------

Jul 22 '05 #4

P: n/a
> But I want to use the pragma once directive with my microsoft compiler. Is
there no other option in to your knowledge?


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


Jul 22 '05 #5

P: n/a
> But I want to use the pragma once directive with my microsoft compiler. Is
there no other option in to your knowledge?


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


Jul 22 '05 #6

P: n/a
"Jacob Jensen" <ja***********************@yahoo.co.uk> schrieb im Newsbeitrag news:nC**************@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

Jul 22 '05 #7

P: n/a
"Jacob Jensen" <ja***********************@yahoo.co.uk> schrieb im Newsbeitrag news:nC**************@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

Jul 22 '05 #8

P: n/a
"<- Chameleon ->" wrote:
Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__


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

#define MY_HEADER_FILE_H

instead.
Jul 22 '05 #9

P: n/a
"<- Chameleon ->" wrote:
Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__


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

#define MY_HEADER_FILE_H

instead.
Jul 22 '05 #10

P: n/a
<- Chameleon -> wrote:
Dont use microsoft specific things

Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__


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.
Jul 22 '05 #11

P: n/a
<- Chameleon -> wrote:
Dont use microsoft specific things

Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__


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.
Jul 22 '05 #12

P: n/a
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #13

P: n/a
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #14

P: n/a
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
"<- Chameleon ->" wrote:
Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__
Double-underscore identifiers are reserved for implementation purposes.

Use something like:

#define MY_HEADER_FILE_H

instead.


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/
Jul 22 '05 #15

P: n/a
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
"<- Chameleon ->" wrote:
Use
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__
Double-underscore identifiers are reserved for implementation purposes.

Use something like:

#define MY_HEADER_FILE_H

instead.


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/
Jul 22 '05 #16

P: n/a
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" <ce******@spamless.rochester.rr.com> wrote in message
news:Ck******************@twister.nyroc.rr.com...
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/

Jul 22 '05 #17

P: n/a
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" <ce******@spamless.rochester.rr.com> wrote in message
news:Ck******************@twister.nyroc.rr.com...
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/

Jul 22 '05 #18

P: n/a
Jacob Jensen wrote:

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" <ce******@spamless.rochester.rr.com> wrote in message
news:Ck******************@twister.nyroc.rr.com...
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/


If you are after efficiency (compilation speed?), look into precompiled
headers.
Jul 22 '05 #19

P: n/a
Jacob Jensen wrote:

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" <ce******@spamless.rochester.rr.com> wrote in message
news:Ck******************@twister.nyroc.rr.com...
"Jacob Jensen" <ja***********************@yahoo.co.uk> wrote in message
news:nC**************@news.get2net.dk...
But I want to use the pragma once directive with my microsoft compiler.


This seems like a very strange requirement. Why?

--
Cy
http://home.rochester.rr.com/cyhome/


If you are after efficiency (compilation speed?), look into precompiled
headers.
Jul 22 '05 #20

P: n/a
Jacob Jensen wrote:
But I want to use the pragma once directive with my microsoft compiler.


Why, when there is a perfectly good, cross-platform, widely-used
convention (i.e. include guards) already out there?

--
Mike Smith

Jul 22 '05 #21

P: n/a
Jacob Jensen wrote:
But I want to use the pragma once directive with my microsoft compiler.


Why, when there is a perfectly good, cross-platform, widely-used
convention (i.e. include guards) already out there?

--
Mike Smith

Jul 22 '05 #22

P: n/a
Jacob Jensen wrote:
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".


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.
Jul 22 '05 #23

P: n/a
Jacob Jensen wrote:
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".


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.
Jul 22 '05 #24

P: n/a
Bill Seurer wrote:
Jacob Jensen wrote:
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".

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).


#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

Jul 22 '05 #25

P: n/a
Bill Seurer wrote:
Jacob Jensen wrote:
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".

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).


#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

Jul 22 '05 #26

P: n/a
Mike Smith wrote:
#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.


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.
Jul 22 '05 #27

P: n/a
Mike Smith wrote:
#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.


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.
Jul 22 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.