Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

c++ - One or more multiply defined symbols found

DebugUtil.h

#ifndef DEBUG_UTIL_H
#define DEBUG_UTIL_H

#include <windows.h>

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

    return 0;
}

#endif

When I try to run this I get this error:

Terrain.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

Renderer.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

test.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

C:UsersTiagoDesktopLoodus EngineDebugLoodus Engine.exe : fatal error LNK1169: one or more multiply defined symbols found

But why does this happen? I have #ifndef #define and #endif in the header so multiple definitions shouldn't happen

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...