0

我正在尝试构建这些文件,但它给了我一个多重定义错误。

主.cpp:

#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"

int main(int argc, char** args) 
{
if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
{
    SDL_Quit();
}

while(core.desiredstate != core.quit)
{
    ::currentstate->EventHandling();
    ::currentstate->Logic();
    core.ChangeState();
    ::currentstate->Render();
    ::currentstate->Update();
}

SDL_FreeSurface(core.screen);
SDL_Quit();

}

核心.cpp:

#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>

/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS) 
{
    //starts SDL subsystems, returns false upon error
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
    return false;
}

//The screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
//Returns false if there was an error
if(screen == NULL)
{
    return false;
}

SDL_WM_SetCaption("Game", NULL);

return true;
}

/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
//original loaded image
SDL_Surface* original = SDL_LoadBMP(filename.c_str());
SDL_Surface* optimized = NULL;

if(original != NULL)
{
    //Sets optimized to optimized version of original
    optimized = SDL_DisplayFormat(original);

    SDL_FreeSurface(original);
}

return optimized;
} 

/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{
//holds the x y coordinates
SDL_Rect location;
location.x = x;
location.y = y;

if(destination != NULL) 
{
    SDL_BlitSurface(source, NULL, destination, &location);
}
}

/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate) 
{
if(desiredstate != state_null && desiredstate != quit)
{
    desiredstate = newstate;
}
}

/* Changes the game state */
void Core::ChangeState()
{
    if(desiredstate != state_null && desiredstate != quit)
    {
        //frees old state memory
    delete ::currentstate;

    switch(desiredstate)
    {
        case intro:
            //allocates new state memory
            ::currentstate = new Intro();
        break;
    }

    stateID = desiredstate;
    desiredstate = state_null;
}
}

游戏状态.h:

#ifndef GAMESTATES_H
#define GAMESTATES_H

class GameStates 
{
public:
    virtual void EventHandling() = 0;
    virtual void Logic() = 0;
    virtual void Render() = 0;
    virtual void Update() = 0;
};

#endif

简介.h:

#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"

class Intro : public GameStates
{
    private:
    SDL_Surface* test;
public:
    Intro();
    void EventHandling();
    void Logic();
    void Render();
    void Update();
    ~Intro();
} intro;

#endif

介绍.cpp:

#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"

Intro::Intro()
{
test = core.Load("test.bmp");
}

void Intro::EventHandling()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
    switch(event.type)
    {
        case SDL_QUIT:
            core.SetState(core.quit);
        break;
    }
}
}

void Intro::Logic()
{
//to be coded when the program actually builds...
}
void Intro::Render()
{
core.ApplySurface(30, 30, test, core.screen);
}

void Intro::Update()
{
SDL_Flip(core.screen);
}

Intro::~Intro()
{
SDL_FreeSurface(test);
}

全局变量.h:

#include "GameStates.h"
#include "SDL/SDL.h"

GameStates* currentstate = NULL;

抱歉,如果缩进关闭;必须放置四个空格才能将其视为有点乱码的代码块。

继承人的错误信息:

/tmp/ccWxKsO5.o:(.bss+0x0): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
/tmp/ccWxKsO5.o:(.bss+0x20): multiple definition of `currentstate'
/tmp/cc13Eqmt.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x0): multiple definition of `intro'
/tmp/ccWxKsO5.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x10): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

生成文件:

OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LIBS = -lSDL

game : $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)

main.o : Core.h GameStates.h globals.h 
    $(CC) $(CFLAGS) main.cpp $(LIBS)

Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
    $(CC) $(CFLAGS) Core.cpp $(LIBS)

Intro.o : Intro.cpp GameStates.h Core.h
    $(CC) $(CFLAGS) Intro.cpp $(LIBS)
4

4 回答 4

1

对于多个翻译单元之间共享的对象,规则是:必须只有一个定义,但您可以有多个声明。

在实践中,这意味着:放“extern Class object;” 在您的 .h 文件中,以及“类对象;” 在您的一个 .CPP 文件中。

例如intro,将 Intro.h 更改为:

class Intro : public GameStates
{
  ... // whatever 
};
extern Intro intro;

并将这一行添加到 Intro.cpp:

Intro intro;

类似地currentstate,在 globals.h 中:

extern GameStates* currentstate;

并在一个 .CPP 中(编译器与哪一个无关):

GateStates* currentstate = NULL;

Ps你的makefile 坏了。您通过-c,这意味着“不链接”到您的链接步骤。尝试这个:

OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL

game : $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)

main.o : Core.h GameStates.h globals.h 
    $(CC) -c $(CFLAGS) main.cpp

Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
    $(CC) -c $(CFLAGS) Core.cpp

Intro.o : Intro.cpp GameStates.h Core.h
    $(CC) -c $(CFLAGS) Intro.cpp
于 2012-09-07T18:19:58.467 回答
1

问题不在于您的代码,而在于您的构建系统。

任何健全的构建系统都会将目标文件的名称与源文件的名称相匹配。但是你有ccWxKsO5.occ13Eqmt.o。更糟糕的是,构建系统似乎试图链接从同一源生成的多个对象(可能有些是由早期运行的编译器创建的)。

tempnam并且通配符*.o不是构建 C++ 程序的合理方式。


好吧,也可能存在一些代码问题。但是一旦错误消息中的对象名称与源文件相关联,查找和修复这些将变得容易一千倍。

于 2012-09-07T18:33:45.180 回答
0

在 globals.h 中包含守卫 make GameStates* 声明 extern

//globals.h
#ifndef _MY_GLOBALS_H_
#define _MY_GLOBALS_H_
#include "GameStates.h"
#include "SDL/SDL.h"

extern GameStates* currentstate;
#endif

//main.cpp

#include "globals.h"

GameStates* currentState = 0;
于 2012-09-07T18:22:24.503 回答
0

在 globals.h 中,您必须声明currentstateextern。然后使用定义 ( GameStates* currentstate = NULL;) 创建 globals.cpp。我在您的代码中找不到任何关于 intro 或 core 的引用,但这可能是同一个问题:只要您将全局变量声明为 extern,您就可以随意声明它们,并且每个生成的二进制文件只定义一次,在只有一个翻译单元。

此外,您可能希望在 globals.h 中添加标头保护 (#ifndef GLOBALS_H ...),以防万一您在其中添加任何其他内容。

于 2012-09-07T18:20:49.837 回答