One or more multiply defined symbols found

后端 未结 9 580
夕颜
夕颜 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 05:14

    100% Certain you correctly included Guards but still getting redefinition error?

    For Visual Studio: I was really frustrated because I was correctly included guards, only to find out the problem was visual studio. If you have added the file to your project, the compiler will add the file twice even if you have include guards around your implementation file and header file.

    If you don't use visual studio exclusively, and say... use code::blocks sometimes, you might want to only #include the file when you detect the absence of the visual studio environment.

    DebugUtil.h :
    ----------------------
    #ifndef _WIN32
    #include "DebugUtil.c"
    #endif
    ----------------------
    

    If you are okay with including stdio.h, you can be a little less hackish about it:

    DebugUtil.h :
    ----------------------
    #include <stdio.h>
    #ifdef _MSC_VER
    #include "DebugUtil.c"
    #endif
    ----------------------
    

    Reference: Predefined Macros, Visual Studio: https://msdn.microsoft.com/en-us/library/b0084kay.aspx

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

    That only prevents multiple inclusions in the same source file; multiple source files #includeing it will still generate multiple definitions of DebugMessage(). In general, you should either not place functions in header files at all or make them static (and usually inline, since otherwise it doesn't usually make sense to have multiple static definitions of the same function).

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

    Move the definition to a .cpp file.

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