函数名__func__的预定义宏 [英] Predefined Macros for function name __func__
问题描述
我正在尝试构建一个调试日志消息函数,记录调用日志消息的文件,行和函数。
#define DEBUG_PANIC(p)CLogging :: Debuglogf(Debug marker(%s) - ::%s文件:%s(%d),p,__func__,__FILE__,__LINE__);
上述代码适用于某些编译器,但不是全部。我的代码需要与GCC以及Microsoft Visual studio交叉兼容。我已经添加了以下定义来帮助兼容性。
#ifndef __FUNCTION_NAME__
#if defined __func__
//未声明
#define __FUNCTION_NAME__ __func__
#elif defined __FUNCTION__
//未声明
#define __FUNCTION_NAME__ __FUNCTION__
#elif已定义__PRETTY_FUNCTION__
//未声明
#define __FUNCTION_NAME__ __PRETTY_FUNCTION__
#else
//已声明
#define __FUNCTION_NAME__N / A
#endif // __func__
#endif // __FUNCTION_NAME__
#define DEBUG_PANIC(p)CLogging :: Debuglogf(Debug marker(%s) - ::%s()in file:%s(%d),p,__FUNCTION_NAME__,__FILE__,__LINE__);
上述代码片段的问题是#else宏在所有编译器上都是活动的其他宏不是。换句话说, #if defined __func __
在编译器上为false,其中 __ func __
是一个预定义的宏 。
我的问题是
- 如何创建交叉编译器宏找到函数名?
- 如何判断是否可以使用
__ func __
?
您假设 __ func __
是一个宏,但它不是。这是一个有条件支持的预定义标识符,因此您不能使用 #if defined
或 #ifdef
检查它。 / p>
如果编译器没有办法告诉你这是否支持(他们可以通过 _FUNC_SUPPORTED
$ b
$ b
#ifndef __FUNCTION_NAME__
#ifdef WIN32 // WINDOWS
#define __FUNCTION_NAME__ __FUNCTION__
#else // * NIX
#define __FUNCTION_NAME__ __func__
#endif
#endif
I am attempting to build a debug log message function that records the file, line, and function of of where the log message was called from.
#define DEBUG_PANIC(p) CLogging::Debuglogf( "Debug marker (%s) - ::%s() in file: %s(%d)", p, __func__ , __FILE__, __LINE__ );
The above code works on some compilers but not all. My code needs to be cross compatible with GCC as well as Microsoft Visual studios. I have added the below defines to help with compatibility.
#ifndef __FUNCTION_NAME__
#if defined __func__
// Undeclared
#define __FUNCTION_NAME__ __func__
#elif defined __FUNCTION__
// Undeclared
#define __FUNCTION_NAME__ __FUNCTION__
#elif defined __PRETTY_FUNCTION__
// Undeclared
#define __FUNCTION_NAME__ __PRETTY_FUNCTION__
#else
// Declared
#define __FUNCTION_NAME__ "N/A"
#endif // __func__
#endif // __FUNCTION_NAME__
#define DEBUG_PANIC(p) CLogging::Debuglogf( "Debug marker (%s) - ::%s() in file: %s(%d)", p, __FUNCTION_NAME__, __FILE__, __LINE__ );
The problem with the above code snippet is that it is the #else macro is active on all compilers while the other macros are not. in other words #if defined __func__
is false on compilers where __func__
is a predefined macro.
My question is
- How do I create a cross compiler macro to find the function name ?
- How can I tell if a
__func__
can be used?
You're assuming __func__
is a macro, but it's not. It's a conditionally-supported predefined identifier, so you can't check it with #if defined
or #ifdef
.
If the compilers have no way of telling you whether this is supported (they could via a _FUNC_SUPPORTED
or something, I'm not saying they actually are doing this), you'll have to check the compiler instead of the actual identifier.
Something along the lines:
#ifndef __FUNCTION_NAME__
#ifdef WIN32 //WINDOWS
#define __FUNCTION_NAME__ __FUNCTION__
#else //*NIX
#define __FUNCTION_NAME__ __func__
#endif
#endif
这篇关于函数名__func__的预定义宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!