我从compilator收到此错误的问题:C++/SDL双列入
1>Linking...
1>main.obj : error LNK2005: "int g_win_flags" ([email protected]@3HA) already defined in init.obj
1>main.obj : error LNK2005: "struct SDL_Surface * g_screen" ([email protected]@[email protected]@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
它看起来像g_win_flags和g_screen两次都包括在内,但我不明白为什么。 这里是源:
的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;
}
有人可以帮忙吗?在此先感谢,并有一个愉快的一天:)
你不应该在除extern声明以外的头文件中定义变量。 – codymanix 2010-11-03 18:26:27
仅供参考,不是编译器错误,它是链接器错误。 – 2010-11-03 18:48:38