How to check if __PRETTY_FUNCTION__ can be used?

拜拜、爱过 提交于 2019-12-12 10:45:42

问题


...../PluginLoader.h:34: multiple definition of '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.

EDIT: 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
    }

回答1:


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



回答2:


__PRETTY_FUNCTION__ and __FUNCTION__ are not preprocessor macros like __LINE__ or __FILE__, but magic constants they are not available at preprocessor time, but later at compile time (in function scope).

So whatever you are trying to do with macros here will probably not work anyway.

However the compiling error is probably a problem with guard. I succeed compiling a not very different program (see below) without any problem. But as I said above, FUNCTION_NAME will always be set to empty string.

xx.h header file

#ifndef H_XX_H
#define H_XX_H

#ifndef FUNCTION_NAME
    void Dummy_Func_For_Generating_FUNCTION_NAME_Macro()
    {
    #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
#endif

xx.c source file

#include <stdio.h>
#include "xx.h"

main(){
    printf("%s\n", FUNCTION_NAME);
}



回答3:


I used to met this problem, which is caused by mounted disk:

subst R: C:\Source\

Test.cpp:

 #include "C:\Source\PluginLoader.h"  
 #include "R:\PluginLoader.h"

Now if you include guard is #pragma once, the compiler is not smart enough to know that they are actual one file, thus cause the redefinition error.

However, I am not sure whether this is your problem, as it depends on:

  • You include from both virtual disk, and physical disk
  • Your include guard is #pragma once, not the macro guard

.




回答4:


Put your function in an anonymous namespace. That will eliminate the duplicate function definition errors. I.e.

 namespace {
     function goes here
  }


来源:https://stackoverflow.com/questions/4164315/how-to-check-if-pretty-function-can-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!