One or more multiply defined symbols found

后端 未结 9 579
夕颜
夕颜 2020-11-30 04:38

DebugUtil.h

#ifndef DEBUG_UTIL_H
#define DEBUG_UTIL_H

#include 

int DebugMessage(const char* message)
{
    const int MAX_CHARS = 1023;
           


        
相关标签:
9条回答
  • 2020-11-30 04:56

    (Assuming the posted code is a header, included from multiple .cpp files)

    Header guards do not protect you from link-time multiple definitions. Regardless that you have ensured the header will only appear once per Translation Unit, if you have more than one Translation Unit then that's still multiple definitions.

    Write definitions in source files, and only declarations in headers.

    The only exceptions are inline functions, functions defined within a class definition (though this is not recommended!) and function templates.

    0 讨论(0)
  • 2020-11-30 04:57

    This function is included into every translation unit and as a result you get multiple definitions of it - each .obj file contains its own copy. When it's time to link them all together the linker rightfully shows the above error.

    You can do a few things:

    1. Move the definition to a .cpp file and keep only the declaration in the header.
    2. Use an anonymous namespace around the function in your header file (but realize it's a hack - you will still have multiple definitions, just no name collision).
    3. Mark it as inline (although it might not always work - only if the compiler actually chooses to inline it). That's also a hack for the same reason as above.
    0 讨论(0)
  • 2020-11-30 05:02

    It looks like you are including DebugUtil.h in more than one translation unit, then linking those objects together. However, DebugUtil.h provides a definition for the DebugMessage function, so that definition exists in all of the translation units that incorporated the header. As a result, when you link the objects, the linker rightly complains that the symbol is multiply defined.

    Change DebugUtil.h so that it declares DebugMessage via a prototype, but does not provide a definition, and place the definition of DebugMessage in a .c file which you will compile and link with your other objects.

    0 讨论(0)
  • 2020-11-30 05:09

    Make the function inline or declare the function in a header file and define it in a cpp file.

    inline int DebugMessage(const char* message)
    {
        const int MAX_CHARS = 1023;
        static char s_buffer[MAX_CHARS+1];
    
        return 0;
    }
    

    EDIT:

    As a comment by Tomalak Geret'kal suggests, it's better to use my latter suggestions than my former and move the function's declaration to a cpp file.

    0 讨论(0)
  • 2020-11-30 05:12

    Put the definition (body) in a cpp file and leave only the declaration in a h file. Include guards operate only within one translation unit (aka source file), not across all your program.

    The One Definition Rule of the C++ standard states that there shall appear exactly one definition of each non-inline function that is used in the program. So, another alternative would be to make your function inline.

    0 讨论(0)
  • 2020-11-30 05:13

    Declare your functions in C++ files. Since you defined your function in the header file, and that header file is included from multiple source files, it gets defined for each source file that includes it. That's why it's reported as being defined in multiple places.

    Alternatively, you could make it inline so that the code is inserted wherever it's used instead of being defined as a separate function each time.

    0 讨论(0)
提交回复
热议问题