pragma once

J

Jacob Jensen

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

Guest

Dont use microsoft specific things

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

// my code here

#endif // __MY_HEADER_FILE_H__
 
J

Jacob Jensen

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
 
G

Guest

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
 
H

Heinz Ozwirk

: 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
 
K

Kevin Goodsell

Dont use microsoft specific things

Use

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
 
C

Cy Edmunds

Julie said:
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.
 
J

Jacob Jensen

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".
 
J

Julie

Jacob said:
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".

If you are after efficiency (compilation speed?), look into precompiled
headers.
 
M

Mike Smith

Jacob said:
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?
 
B

Bill Seurer

Jacob said:
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.
 
M

Mike Smith

Bill said:
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.
 
J

Julie

Mike said:
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top