【发布时间】:2013-02-24 15:58:09
【问题描述】:
我正在尝试构建一个调试日志消息函数,用于记录调用日志消息的文件、行和函数。
#define DEBUG_PANIC(p) CLogging::Debuglogf( "Debug marker (%s) - ::%s() in file: %s(%d)", p, __func__ , __FILE__, __LINE__ );
上面的代码适用于一些编译器,但不是全部。我的代码需要与 GCC 以及 Microsoft Visual Studio 交叉兼容。我添加了以下定义以帮助兼容性。
#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__ );
上述代码 sn-p 的问题在于 #else 宏在所有编译器上都处于活动状态,而其他宏则不处于活动状态。换句话说,#if defined __func__ 在 __func__ 是 预定义宏 的编译器上为 false。
我的问题是
- 如何创建交叉编译器宏来查找函数名?
- 如何判断
__func__是否可以使用?
【问题讨论】:
-
你在写C还是C++?
-
@KeithThompson 抱歉,我忘了在问题中添加这个,是的 C++
标签: c++ visual-studio gcc c-preprocessor