GCC __func__被评估为空字符串

use*_*550 1 c gcc string-literals c-preprocessor

在我正在研究的项目中给出以下代码:

/* Pre-definitions in a pre-definitions file to be included in the project */
#ifdef  WIN32
#define __FUNCNAME__ __FUNCTION__
#else
#define __FUNCNAME__ __func__  
#endif

/* My definitions */
#define MAC() \
     MAC1()

#define MAC1() \
     myPrintFunction(__FUNCNAME__)

/* My print function */
void myPrintFunction(const char * functionName)
{
     printf("\n func name: %s \n",functionName);
}

/* Macro usage example function */
void myFunction()
{
     if (some_condition)
     {
         MAC();
     }
}
Run Code Online (Sandbox Code Playgroud)

函数名称显示为空字符串.知道为什么,我该如何解决?

使用GCC编译器在Linux机器上编译和测试代码.

Bat*_*eba 5

使用__func__开箱即用.自C99以来,它一直是C标准的一部分.将您的编译器设置更改为至少使用该标准.

注意,__func__宏而是一个预定的标识符,其采取的形式,使得功能体中的任何位置写入它是完全等同于使用它在这一点上,具有第一写

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

就在功能体的开口支撑之后.

正式地,您当前代码的行为是未定义的.系统保留包含两个连续下划线的任何符号.(包括宏名称,函数名称和变量名称.)