0
votes

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.

2
why are you include SDL.h and SDL_opengl again in main?Ali Kazmi
Fixed that @AliKazmi, thank youThums

2 Answers

3
votes

Your main.cpp file should include engine.h, not engine.cpp.

Further, you do define the global e multiple times, in every translation unit that includes the engine.h header. In the engine.h header declare it extern instead, which tells the compiler that e exists somewhere but doesn't actually define it:

extern SDL_Event e;

Then define it in only one translation unit; put this in engine.cpp:

SDL_Event e;

You need to do the same thing with all of the other global variables as well. Note that you need to initialize them where you define them, so this goes in the header:

extern bool running, attack, up, down, left, right;

And this goes in engine.cpp:

bool running = true;
bool attack = false; /* and so on */
1
votes

You are getting these redefinition issues as you have included .cpp file in your main:-

#include "engine.cpp"

You should always include header files only. ( Also these header files should contain appropriate header gaurds ).