Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One or more multiply defined symbols found

Tags:

c++

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:\Users\Tiago\Desktop\Loodus Engine\Debug\Loodus 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

like image 484
Tiago Costa Avatar asked Jun 24 '11 15:06

Tiago Costa


People also ask

How do I fix lnk1169 error?

(7) This error can occur if the symbol is defined differently in two member objects in different libraries, and both member objects are used. One way to fix this issue when the libraries are statically linked is to use the member object from only one library, and include that library first on the linker command line.


1 Answers

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.

like image 96
Armen Tsirunyan Avatar answered Sep 22 '22 21:09

Armen Tsirunyan