How to check if __PRETTY_FUNCTION__ can be used?

10,765

Solution 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

Solution 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);
}
Share:
10,765
nakiya
Author by

nakiya

Feeling lost in the land of gods

Updated on June 09, 2022

Comments

  • nakiya
    nakiya over 1 year

    ...../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
        }
    
  • Paul R
    Paul R almost 13 years
    But __PRETTY_FUNCTION__, __FUNCTION__ and __func__ are only ever defined within a function !
  • ed9w2in6
    ed9w2in6 almost 13 years
    @Paul R, macro define should have no scope
  • Dialecticus
    Dialecticus almost 13 years
    Use FUNCTION_NAME in a function that you're actually interested in, and not in dummy function, where it will always resolve to dummy.
  • nakiya
    nakiya almost 13 years
    @Dialecticus: I'm just ensuring that FUNCTION_NAME is defined. Exactly because preprocessor runs before compiler, I can know what macro to use.
  • Dialecticus
    Dialecticus almost 13 years
    It is not possible to do that. You must check the compiler (each has special identifying macro, and a version as well), instead of checking the existence of function-scope macros.
  • nakiya
    nakiya almost 13 years
    @Dialecticus: Which is besides the point. Why does the function definition give an error?
  • Dialecticus
    Dialecticus almost 13 years
    Put the function definition in .cpp, and only function declaration in .h
  • GManNickG
    GManNickG almost 13 years
    __GUARD is reserved, don't use it.
  • kriss
    kriss almost 13 years
    OK, I guess you are not reacting over the __guard keyword but on the double leading underscore.
  • Goz
    Goz almost 13 years
    +1: This answer is spot on. Drop the function. It is multiply defined because you are putting it in a header file without an inline. The methodology described in the answer is the perfect solution, IMO :)
  • nakiya
    nakiya almost 13 years
    @Goz : So, if I inline the function, will the error go away? I am not planning to use that method, just want to know. Why is it so?
  • Dialecticus
    Dialecticus almost 13 years
    Answer from kriss explains that __FUNCTION__ is not really a preprocessor macro. It is defined in compile time, and not in preprocessing time. Preprocessor does not recognize it.
  • Dialecticus
    Dialecticus almost 13 years
    Kriss' answer is the actual answer to your question. I merely tried to give you the answer to a question you sould have really asked.
  • nakiya
    nakiya almost 13 years
    +1. I'm going to accept this though there should be some way to offer acceptance to more than one answer in which case I could have accepted Kriss' answer as well. Thanks for the help.
  • Gearoid Murphy
    Gearoid Murphy almost 12 years
    +1, I did not realise that the PRETTY_FUNCTION et al constants were not initialised until compile time, in retrospect, it seems obvious :)