开发者

How to check if __PRETTY_FUNCTION__ can be used?

开发者 https://www.devze.com 2023-01-24 13:46 出处:网络
...../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

...../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
    }


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__ 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);
}


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

.


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

 namespace {
     function goes here
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消