One or more multiply defined symbols found

后端 未结 9 623
夕颜
夕颜 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: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.

提交回复
热议问题