pragma一次 [英] pragma once

查看:61
本文介绍了pragma一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在创建多平台可编译的C ++代码时遇到问题。我的

问题是使用#pragma once微软可以使用的指令

Visual Studio预编译器,但是当gcc用于

示例时会发出警告。我想在使用Visual C ++编译器编译时使用它,但

如何创建一个智能宏或其他东西,以便它真正被忽略

编译时使用gcc 。我已经尝试了以下实际上不用的功能

满足我的愿望:


1-

#ifdef VISUAL_STUDIO_COMPILER

#define MY_ONCE一次

#else

#define MY_ONCE

#endif


然后使用它我只需输入:

#pragma MY_ONCE


但是当使用带有-Wall选项的gcc编译时,这仍会发出警告/>
给出。


2-

在pragma_once.h文件中输入以下内容

#ifdef VISUAL_STUDIO_COMPILER

#pragma一次

#endif


然后每当我需要它时,我都会包含pragma_once.h。这个

的问题在于它不起作用,因为pragma once指令只对它所在的文件有效,在这种情况下为pragma_once.h


3-

我没有任何其他想法,但以某种方式定义宏如下:

#ifdef VISUAL_STUDIO_COMPILER

#define PRAGMA_ONCE" #pragma once"

#else

#define PRAGMA_ONCE

#endif


但这不能编译,我发现用

类似的结构编译它是不可能的。


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.


推荐答案

不要使用微软特定的东西


使用

----------- ------

#ifndef __MY_HEADER_FILE_H__

#define __MY_HEADER_FILE_H__


//我的代码在这里


#endif // __MY_HEADER_FILE_H__

-----------------

而不是

-----------------

#pragma曾经


//我的代码在这里

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


但是我想在我的microsoft编译器中使用pragma once指令。是否

你知道其他选择吗?


提前再次感谢

雅各布


"< - Chameleon - >" < CH ****** @ hotmail.NOSPAM.com>在消息中写道

新闻:c4 ********** @ nic.grnet.gr ...
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...
不要使用微软特定的东西

使用
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__

//我的代码在这里

#endif // __MY_HEADER_FILE_H__
-----------------
而不是
--- --------------
#pragma曾经

//我的代码在这里
------------ -----
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
-----------------



但是我想用myoft编译器使用pragma once指令。是否

你知道其他选择吗?


提前再次感谢

雅各布


"< - Chameleon - >" < CH ****** @ hotmail.NOSPAM.com>在消息中写道

新闻:c4 ********** @ nic.grnet.gr ...
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...
不要使用微软特定的东西

使用
-----------------
#ifndef __MY_HEADER_FILE_H__
#define __MY_HEADER_FILE_H__

//我的代码在这里

#endif // __MY_HEADER_FILE_H__
-----------------
而不是
--- --------------
#pragma曾经

//我的代码在这里
------------ -----
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
-----------------



这篇关于pragma一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆