DebugUtil.h
#ifndef DEBUG_UTIL_H
#define DEBUG_UTIL_H
#include
int DebugMessage(const char* message)
{
const int MAX_CHARS = 1023;
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
That only prevents multiple inclusions in the same source file; multiple source files #include
ing 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).
Move the definition to a .cpp file.