I searched and found various similar questions though I wasn't able to find a solution for my problem. It's a SDL2 + OpenGL program, I can compile it with no problems in Linux using g++ 4.9.1 though not on Windows (VS 2013).
I get errors like:
Error 1 error LNK2005: "union SDL_Event e" (?e@@3TSDL_Event@@A) already defined in engine.obj PATH_TO_PROJECT\main.obj Game
for all the variables defined in the file engine.h:
//engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include <SDL.h>
#include <SDL_opengl.h>
#include <iostream>
#include "player.cpp"
SDL_Event e;
bool running = true;
bool up = false, down = false, left = false, right = false;
bool attack = false;
player hero(20, 300, 50, 50, 10.0); //x, y, lenght, height, speed
void init(char* title, int WIDTH, int HEIGHT);
void draw(SDL_Window* screen, SDL_GLContext context, int WIDTH, int HEIGHT);
#endif
engine.cpp consists of:
//engine.cpp
#include "engine.h"
void init(int WIDTH, int HEIGHT) {
//BODY OF THE FUNCTION
}
void draw(SDL_Window* screen, SDL_GLContext context, int WIDTH, int HEIGHT) {
//BODY OF THE FUNCTION
}
main.cpp is the only file that includes engine.cpp:
//main.cpp
#include <SDL.h>
#include <SDL_opengl.h>
#include "engine.cpp"
#include <iostream>
#define WIDTH 800
#define HEIGHT 600
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
STD::cout << "SDL started." << STD::endl;
init(WIDTH, HEIGHT);
//Create the window
SDL_Window *screen = SDL_CreateWindow("game title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(screen);
draw(screen, context, WIDTH, HEIGHT);
SDL_Quit();
return 0;
}
I also get these:
Warning 9 warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library PATH_TO_PROJECT\MSVCRTD.lib(cinitexe.obj) Game
Error 10 error LNK2019: unresolved external symbol _SDL_main referenced in function _main PATH_TO_PROJECT\SDL2main.lib(SDL_windows_main.obj) Game
Error 11 error LNK1120: 1 unresolved externals PATH_TO_PROJECT\Game.exe Game
I can't really understand what is going on, specially since I can compile it on Linux, could someone please help me out? The libraries are corrected linked, verified that. Also, if I put all the code in the main function and use only one file it compiles and runs with no problem.