I would like to know what is causing a double definition linker error in my code. It confuses me because there doesn't seem to be any reason for it. I don't think I'm #include
ing any .h
files in places that would cause this, as my file's #include
s are linearly structured.
To be clear, I am not asking what the errors mean, I am asking what, in my code, is causing it. I have already researched on this site (and others) as to what it means, and can't find an answer relevant to what could be causing it in my code.
Btw I am using Visual Studio 2017
Here are the errors:
1>ObjectHandler.cpp
1>Main.cpp
1>Generating Code...
1>ObjectHandler.obj : error LNK2005: "void __cdecl pixelsInit(void)" (?
pixelsInit@@YAXXZ) already defined in Main.obj
1>ObjectHandler.obj : error LNK2005: "void __cdecl pixelsUpdate(void)" (?
pixelsUpdate@@YAXXZ) already defined in Main.obj
1>ObjectHandler.obj : error LNK2005: "void __cdecl spritesInit(void)" (?
spritesInit@@YAXXZ) already defined in Main.obj
1>ObjectHandler.obj : error LNK2005: "void __cdecl spritesUpdate(void)" (?
spritesUpdate@@YAXXZ) already defined in Main.obj
1>ObjectHandler.obj : error LNK2005: "class std::vector<struct pixel,class
std::allocator<struct pixel> > pixels" (?pixels@@3V?$vector@Upixel@@V?
$allocator@Upixel@@@std@@@std@@A) already defined in Main.obj
1>ObjectHandler.obj : error LNK2005: "class std::map<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct sprite,struct std::less<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >,class std::allocator<struct std::pair<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > const ,struct sprite> > > sprites" (?sprites@@3V?
$map@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@Usprite@@U?$less@V?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?
$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@Usprite@@@std@@@2@@std@@A) already defined in Main.obj
1>fatal error LNK1169: one or more multiply defined symbols found
Here is my code structure:
SimpleTypes.h:
#pragma once
struct loc {
int x;
int y;
int d;
int s;
};
struct color {
_int8 r;
_int8 g;
_int8 b;
_int8 a;
};
ComplexTypes.h:
#pragma once
#include "SimpleTypes.h"
#include <string>
struct pixel {
loc pos;
color col;
};
struct sprite {
std::string name;
std::string file;
loc pos;
};
ObjectHandler.cpp:
#include "ComplexTypes.h"
#include <string>
#include <map>
#include <vector>
std::vector<pixel> pixels;
std::map<std::string, sprite> sprites;
void pixelsInit(){};
void spritesInit(){};
void pixelsUpdate(){};
void spritesUpdate(){};
Main.cpp
#include "ObjectHandler.cpp"
int main() {
pixelsInit();
spritesInit();
while (true) {
pixelsUpdate();
spritesUpdate();
};
return 0;
}