C语言 为宏 __FUNCTION__ 和 __func__ 定义#define 的跨平台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:31:46  来源:igfitidea点击:

Cross-platform defining #define for macros __FUNCTION__ and __func__

cvisual-studiomacros

提问by ant2009

Compiling with gcc 4.4.2 and WinXP Visual Studio C++ 2008

使用 gcc 4.4.2 和 WinXP Visual Studio C++ 2008 进行编译

#if defined ( WIN32 )
#define __FUNCTION__ __func__
#endif

As I want to use the macro to display the function name. I have done the above so I can cross-platform, and use the same funcwhen compiling on linux or windows.

因为我想使用宏来显示函数名称。我已经完成了上述操作,因此我可以跨平台,并在 linux 或 windows 上编译时使用相同的func

However, when I am compiling on WinXP I get the following error:

但是,当我在 WinXP 上编译时,出现以下错误:

__func__ undeclared identifier

Can I not #define a macro like this?

我不能#define 这样的宏吗?

Many thanks for any suggestions,

非常感谢您的任何建议,

回答by tomlogic

It looks like you have your #definebackward. If you want to use __func__on both platforms, and WIN32 has __FUNCTION__but not __func__, you need to do this instead:

看起来你有你的#define落后。如果你想__func__在两个平台上使用,而 WIN32 有__FUNCTION__但没有__func__,你需要这样做:

#if defined ( WIN32 )
#define __func__ __FUNCTION__
#endif

There may be a better way to know whether you need to define __func__or not, but this quick hack should do the trick.

可能有更好的方法来知道您是否需要定义__func__,但是这个快速的 hack 应该可以解决问题。

Remember, on compilers that support the __FUNCTION__and __func__keywords, they're not macros so you can't do the following (since #ifndef __func__isn't valid):

请记住,在支持__FUNCTION__and__func__关键字的编译器上,它们不是宏,因此您不能执行以下操作(因为#ifndef __func__无效):

#ifndef __func__
#define __func__ __FUNCTION__
#endif

From the C99 spec:

从 C99 规范:

6.4.2.2 Predefined identifiers

1 The identifier __func__shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

6.4.2.2 预定义标识符

1 标识符__func__应由翻译器隐式声明,就好像,紧跟在每个函数定义的左大括号之后,声明

static const char __func__[] = "function-name";

出现了,其中 function-name 是词法封闭函数的名称。

回答by Hans Passant

The __FUNCTION__macro is pre-defined in the MSVC compiler. You'll need to make it look like this:

__FUNCTION__宏是在 MSVC 编译器中预定义的。你需要让它看起来像这样:

#ifndef _MSC_VER
#define __FUNCTION__ __func__
#endif

Or the other way around, if you prefer:

或者反过来,如果您愿意:

#ifdef _MSC_VER
#define __func__ __FUNCTION__
#endif

回答by Bill Lynch

You should be able to use __func__without any explicit macros in any compiler that supports C99.

__func__在支持 C99 的任何编译器中,您应该能够在没有任何显式宏的情况下使用。

回答by sbi

You can of course #definesuch a macro. Every instance of FUNCTIONis then replaced by __func__. However, obviosuly your compiler doesn't know __func__. I believe VC knows __FUNCTION__, so

你当然可以#define这样的宏。FUNCTION然后将 的每个实例替换为__func__。但是,显然您的编译器不知道__func__. 我相信VC知道__FUNCTION__,所以

#if defined ( WIN32 )
#  define __func__ __FUNCTION__
#endif

might do.

可能做。