开发者

C++/SDL problem of double inclusion

I'm getting this error from compilator:

1>Linking...
1>main.obj : error LNK2005: "int g_win_flags" (?g_win_flags@@3HA) already defined in init.obj
1>main.obj : error LNK2005: "struct SDL_Surface * g_screen" (?g_screen@@3PAUSDL_Surface@@A) already defined in init.obj
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>.\Debug\Heroes are back!.exe : fatal error LNK1169: one or more multiply defined symbols found

It looks like that g_win_flags and g_screen are twice included, but I don't understand why. Here is the source:

main.cpp

#include <iostream>
#include "dec.h"
#include "init.h"

int main(int argc, char *argv[]){

    init();
    return 0;
}

dec.h

#ifndef DEC_H
#define DEC_H

#include <SDL.h>
#include <iostream>

#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

using namespace std;

int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;

SDL_Surface *g_screen = NULL;

#endif

init.h

#ifndef INIT_H
#define INIT_H

bool init();

#endif

init.开发者_运维知识库cpp

#include "dec.h"

bool init(){
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1){
        cerr << "Unable to initialize SDL" << endl;
        return false;
    }
    g_screen = SDL_SetVideoMode(640, 480, 0, g_win_flags);

    return true;
}

Can someone help? Thanks in advance and have a nice day :)


You define and initialize the variables in the header.

You should just declare them in the header (dec.h) without any initializers:

extern int g_win_flags;
extern SDL_Surface *g_screen;

Then define them once in one file - presumably dec.cpp - with the initializations.

As it was, you were defining them in every source file that included 'dec.h' and then running foul of the ODR - One Definition Rule.


in dec.h you want

extern int g_win_flags;

extern SDL_Surface *g_screen;

and then to define and initalise them in just dec.cpp

Update:

#include "dec.h"
int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;

SDL_Surface *g_screen = NULL;

The general rule of thumb is "nothing in a header file should take up any space in the output of the compiler". (There are exceptions to this obviously)

In practice this means extern variable declarations are fine, as are function declarations, but not definitions.


You have included files into two different source files (init.cpp and main.cpp) that define instantiated variables.

You need a way to be sure they are 'externed' in all but one source file.


Well, I tried to do what you've told me guys, but compilator is complaining:

1>.\heroes are back!\dec.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C2040: 'g_screen' : 'int' differs in levels of indirection from 'SDL_Surface *'

Here is the dec.h

#ifndef DEC_H
#define DEC_H

#include <SDL.h>
#include <iostream>

#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

using namespace std;

extern int g_win_flags;

extern SDL_Surface *g_screen;

#endif

dec.cpp

#include "dec.h"
g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;

g_screen = NULL;

What is wrong in this code? Sorry for asking dumb questions, but I'm only learning C++ :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜