C++/SDL 双重包含问题

发布于 2024-09-30 04:07:18 字数 1486 浏览 6 评论 0原文

我从编译器中收到此错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も星光 2024-10-07 04:07:18

您可以在标头中定义并初始化变量。

您应该只在标头 (dec.h) 中声明它们,而不使用任何初始化程序:

extern int g_win_flags;
extern SDL_Surface *g_screen;

然后使用初始化在一个文件(大概是 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:

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.

☆獨立☆ 2024-10-07 04:07:18

在 dec.h 中

extern int g_win_flags;

extern SDL_Surface *g_screen;

,然后在 dec.cpp 中定义和初始化它们

更新:

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

SDL_Surface *g_screen = NULL;

一般经验法则是“头文件中的任何内容都不应占用编译器输出中的任何空间”。 (显然也有例外)

实际上,这意味着 extern 变量声明可以,函数声明也可以,但定义不行。

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.

囚我心虐我身 2024-10-07 04:07:18

您已将文件包含到定义实例化变量的两个不同的源文件(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.

悟红尘 2024-10-07 04:07:18

好吧,我试着做你们告诉我的事情,但是编译器抱怨:

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 *'

这是 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;

这段代码有什么问题?抱歉问了愚蠢的问题,但我只是在学习 C++ :)

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++ :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文