473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.


Jul 22 '05 #1
27 8557
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
-----------------
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******@hotma il.NOSPAM.com> wrote in message
news:c4******** **@nic.grnet.gr ...
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
-----------------

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******@hotma il.NOSPAM.com> wrote in message
news:c4******** **@nic.grnet.gr ...
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
-----------------

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************ ***********@yah oo.co.uk> schrieb im Newsbeitrag news:nC******** ******@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

Jul 22 '05 #7
"Jacob Jensen" <ja************ ***********@yah oo.co.uk> schrieb im Newsbeitrag news:nC******** ******@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

Jul 22 '05 #8
"<- Chameleon ->" wrote:
Use
-----------------
#ifndef __MY_HEADER_FIL E_H__
#define __MY_HEADER_FIL E_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_FIL E_H__
#define __MY_HEADER_FIL E_H__


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

#define MY_HEADER_FILE_ H

instead.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2144
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 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...
6
3957
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
187
6424
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 at C# which is much cleaner with namespaces. Why has C not namespaces and a "area idea" where some methods and
8
2230
by: karunesh | last post by:
why this #pragma use very extnsivly please tell me the usses thaks!! Regards, Karuensh
7
15342
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 dependent, meaning that it will be handled differently depending on the compiler? #ifndef is not dependent on the compiler. So from that i take it that its more universal to use #ifndef because its the standard, but if I use #pragma once in visual...
26
3808
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? Thanks...
0
10679
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 the Oracle compiler that tells it to do something. In this case you are telling Oracle to associate an error that you choose to a variable that you choose. This pragma must go in the declarative section of your block. Types of PRAGMA...
5
25208
by: raashid bhatt | last post by:
What is #pragma once used for and what is #WIN#@_LEN_AND_MEAN
3
3893
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? Thanks in Advance, Subrat
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8352
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6115
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.