7

...../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
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • what is the value of `__PRETTY_FUNCTION__`, `__FUNCTION__` and `__func__` ? – Adrien Plisson Nov 12 '10 at 12:03
  • @Adrien: these are predefined macros, but not all are implemented for all compilers. – Paul R Nov 12 '10 at 12:05
  • 1
    Did you place this function in a header? If the header is included multiple times, the compiler may complain that it finds the same function more than once. – Patrick Nov 12 '10 at 12:09
  • 4
    [Ask about the goal, not the step.](http://www.catb.org/esr/faqs/smart-questions.html) Nobody can help you if they don't understand what you're trying to do. So "What's wrong here" is "Who knows? You tell us." As far as I know your goal is to generate compile errors; nothing wrong here. – GManNickG Nov 12 '10 at 12:14
  • 1
    @GMan: I am not trying to produce compiler errors. I am just trying to get `FUNCTION_NAME` defined correctly. And I have clearly asked my question in the title. – nakiya Nov 12 '10 at 12:18
  • Even if you fix the problem `FUNCTION_NAME` will always be defined as `Dummy_Func_For_Generating_FUNCTION_NAME_Macro`, no matter where you use it. Is that what you want? – Dialecticus Nov 12 '10 at 12:37
  • @nakiya: No, you haven't. Your title is unclear. What "this macro" are you talking about? I see a function, and some macros that happen to reside in it. **Why** are they there? **What** are you attempting to do that made you do that? Those are the things you need to explain. – GManNickG Nov 12 '10 at 12:48
  • @GMan: Edited. Hope that makes things clearer. – nakiya Nov 12 '10 at 13:13
  • @nakiya: where in the title does it say "I am just trying to get `FUNCTION_NAME` defined correctly? You asked your question in a comment. The actual question body and title didn't tell us what you were trying to do. – jalf Nov 12 '10 at 13:54
  • @jalf: Well, for all this nit-picking, no one gave me a hint how to do what I want :). – nakiya Nov 12 '10 at 14:45
  • @nakiya: The solution is in my answer. Use `_MSC_VER` to identify `__FUNCTION__`. Handle all other compilers accordingly. – Dialecticus Nov 18 '10 at 09:43

4 Answers4

14

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
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • But `__PRETTY_FUNCTION__`, `__FUNCTION__` and `__func__` are only ever defined *within a function* ! – Paul R Nov 12 '10 at 12:10
  • @Paul R, macro define should have no scope – Baiyan Huang Nov 12 '10 at 12:14
  • Use `FUNCTION_NAME` in a function that you're actually interested in, and not in dummy function, where it will always resolve to dummy. – Dialecticus Nov 12 '10 at 12:14
  • 2
    @Dialecticus: I'm just ensuring that `FUNCTION_NAME` is defined. Exactly because preprocessor runs before compiler, I can know what macro to use. – nakiya Nov 12 '10 at 12:16
  • 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. – Dialecticus Nov 12 '10 at 12:29
  • @Dialecticus: Which is besides the point. Why does the function definition give an error? – nakiya Nov 12 '10 at 12:34
  • Put the function definition in .cpp, and only function declaration in .h – Dialecticus Nov 12 '10 at 12:38
  • +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 :) – Goz Nov 18 '10 at 09:35
  • @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? – nakiya Nov 18 '10 at 12:59
  • 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 Nov 18 '10 at 13:04
  • 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. – Dialecticus Nov 18 '10 at 13:07
  • +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. – nakiya Nov 18 '10 at 13:19
5

__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);
}
kriss
  • 23,497
  • 17
  • 97
  • 116
  • `__GUARD` [is reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier), don't use it. – GManNickG Nov 12 '10 at 12:46
  • OK, I guess you are not reacting over the `__guard` keyword but on the double leading underscore. – kriss Nov 12 '10 at 13:14
  • +1, I did not realise that the __PRETTY_FUNCTION__ et al constants were not initialised until compile time, in retrospect, it seems obvious :) – Gearoid Murphy Dec 16 '11 at 14:20
0

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

 namespace {
     function goes here
  }
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
0

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

.

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72