473,414 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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.


Jul 22 '05 #1
27 8518
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
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
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
> 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
> 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
"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
"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
"<- 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
"<- 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
<- 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
<- 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
"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
"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
"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
"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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacob Jensen | last post by:
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...
6
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
187
by: Lasse Espeholt | last post by:
Hi... I am relativ new to the impressive and powerfull C language, but i thinks it is obsolete... The idea with header/source files where methods can clash into eachother i don't like... Look...
8
by: karunesh | last post by:
why this #pragma use very extnsivly please tell me the usses thaks!! Regards, Karuensh
7
by: Studlyami | last post by:
Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef. From my understanding #pragma is compiler...
26
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? ...
0
debasisdas
by: debasisdas | last post by:
PRAGMA:-Signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They pass information to the compiler. A pragma is an instruction to...
5
by: raashid bhatt | last post by:
What is #pragma once used for and what is #WIN#@_LEN_AND_MEAN
3
by: Subrat | last post by:
Hello, I have used #pragma once in a header file. During compilation I get a warning : #pragma once is obsolete. How do I switch off this warning, without making any change to my make file? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.