如何检查是否可以使用__PRETTY_FUNCTION__? [英] How to check if __PRETTY_FUNCTION__ can be used?
问题描述
..... / PluginLoader.h:34:'Dummy_Func_For_Generating_FUNCTION_NAME_Macro()'的多重定义
以下代码输出上述错误。我在我的文件中包含警卫。和一切编译良好。
The above error is output for the below code. I have include guards in my file. And everything else compiles fine.
编辑:我想要实现的是检查是否定义了 __ PRETTY_FUNCTION __
,如果是,稍后在代码中通过 FUNCTION_NAME
宏(用于日志记录)。如果未定义 __ PRETTY_FUNCTION __
,请使用下一个最好的东西,以此类推。然而,我的回答让我意识到,这不可能。所以,如果 __ PRETTY_FUNCTION __
,所有这些其他都不是宏,它们是什么?如何检查某个实现是否有其中一个实现?
What I was trying to achieve was to check if __PRETTY_FUNCTION__
was defined, and if it was, use it later in code via FUNCTION_NAME
macro (For logging purposes). If __PRETTY_FUNCTION__
is not defined, use next best thing and so on. However, the responses I got made me realize that this impossible. So, if __PRETTY_FUNCTION__
and all these others are not macros, what are they? And how do I check if a certain implementation has one of them or not?
void Dummy_Func_For_Generating_FUNCTION_NAME_Macro()
{
#ifndef FUNCTION_NAME
#ifdef __PRETTY_FUNCTION__
#define FUNCTION_NAME __PRETTY_FUNCTION__
#elif __FUNCTION__
#define FUNCTION_NAME __FUNCTION__
#elif __func__
#define FUNCTION_NAME __func__
#else
#define FUNCTION_NAME ""
#endif
#endif
}
推荐答案
void Dummy_Func_For_Generating_FUNCTION_NAME_Macro()
是一个函数,函数不创建宏。宏在预处理阶段解析,在编译阶段函数。删除函数定义并只保留 #ifndef
块。
void Dummy_Func_For_Generating_FUNCTION_NAME_Macro()
is a function, not a macro. Functions don't create macros. Macros are resolved in preprocessor phase, and functions in compiler phase. Remove the function definition, and leave only #ifndef
block.
宏来确定要使用的功能识别宏。例如:
Use compiler identifying macros to figure out which function identifying macro to use. For instance:
#ifdef _MSC_VER // Visual Studio
#define FUNCTION_NAME __FUNCTION__
#endif
这篇关于如何检查是否可以使用__PRETTY_FUNCTION__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!