我從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