C++/SDL 双重包含问题
我从编译器中收到此错误:
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
看起来 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;
}
有人可以帮忙吗?提前致谢,祝您有美好的一天:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在标头中定义并初始化变量。
您应该只在标头 (dec.h) 中声明它们,而不使用任何初始化程序:
然后使用初始化在一个文件(大概是 dec.cpp)中定义它们一次。
事实上,您在包含“dec.h”的每个源文件中定义它们,然后违反了 ODR - 单一定义规则。
You define and initialize the variables in the header.
You should just declare them in the header (dec.h) without any initializers:
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.
在 dec.h 中
,然后在 dec.cpp 中定义和初始化它们
更新:
一般经验法则是“头文件中的任何内容都不应占用编译器输出中的任何空间”。 (显然也有例外)
实际上,这意味着 extern 变量声明可以,函数声明也可以,但定义不行。
in dec.h you want
and then to define and initalise them in just dec.cpp
Update:
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.
您已将文件包含到定义实例化变量的两个不同的源文件(init.cpp 和 main.cpp)中。
您需要一种方法来确保它们在除一个源文件之外的所有源文件中都是“外部”的。
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.
好吧,我试着做你们告诉我的事情,但是编译器抱怨:
这是 dec.h
dec.cpp
这段代码有什么问题?抱歉问了愚蠢的问题,但我只是在学习 C++ :)
Well, I tried to do what you've told me guys, but compilator is complaining:
Here is the dec.h
dec.cpp
What is wrong in this code? Sorry for asking dumb questions, but I'm only learning C++ :)