Allgro 5中的定义错误“ ...的多个定义在这里首先定义...”

问题描述

我尝试编译我的allegro 5程序,但是由于某种原因我得到了它

我想制作一种类似于引擎的结构,所以我制作了一个应用类,以使其更容易制作像ludum dare这样的游戏

所以您将创建一个类并使其继承自应用程序,只需填写更新init和draw方法

我真的是从终端进行编译的初学者(我使用linux)

我的构建脚本如下

gcc src / *。cpp build / Game $(pkg-config allegro-5 allegro_font-5 --libs --cflags) ./build/Game 暂停

希望我得到答案

/usr/bin/ld: /tmp/ccbdAHA8.o: in function `Pislify::DefaultwindowConfig()':
main.cpp:(.text+0x0): multiple deFinition of `Pislify::DefaultwindowConfig()'; /tmp/ccwYzbn9.o:Engine.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccwYzbn9.o:(.data.rel.ro._ZTIN7Pislify4GameE[_ZTIN7Pislify4GameE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2: error: ld returned 1 exit status

我的代码: Engine.hpp

#ifndef ENGINE_HPP
#define ENGINE_HPP

#include <allegro5/allegro5.h>
#include <allegro5/allegro_font.h>

namespace Pislify
{
    struct WindowConfiguration
    {
        int WindowWidth;
        int WindowHeight;
        char* WindowTitle;
        int FPS;
    };
    WindowConfiguration DefaultwindowConfig()
    {
        WindowConfiguration conf;
        conf.WindowWidth = 1280;
        conf.WindowHeight = 720;
        conf.FPS = 60;
        conf.WindowTitle = "game";
    }
    class Game
    {
        WindowConfiguration config;
        void Start();

        ALLEGRO_TIMER* timer;
        ALLEGRO_EVENT_QUEUE* queue;
        ALLEGRO_disPLAY* disp;
        ALLEGRO_FONT* font;

    public:
        ALLEGRO_EVENT event;
        Game(WindowConfiguration c);
        Game(int w,int h,char* t);
        virtual void Init(){}
        virtual void Update(){}
        virtual void Draw(){}
    };
}

#endif

engine.cpp

#include "Engine.hpp"
using namespace Pislify;
void Game::Start()
{
    //Initialize allegro
    al_init();
    al_install_keyboard();

    //initalize Allegro's Vars
    timer = al_create_timer(1.0 / config.FPS);
    queue = al_create_event_queue();
    disp = al_create_display(config.WindowWidth,config.WindowHeight);
    font = al_create_builtin_font();

    //some more al config
    al_register_event_source(queue,al_get_keyboard_event_source());
    al_register_event_source(queue,al_get_display_event_source(disp));
    al_register_event_source(queue,al_get_timer_event_source(timer));

    //game loop
    Init();
    
    bool ShouldClose;
    al_start_timer(timer);

    while(!ShouldClose)
    {
        al_wait_for_event(queue,&event);
        
        Update();

        //time to draw? draw!
        if(event.type == ALLEGRO_EVENT_TIMER)
        {
            Draw();
        }

        //close Window?
        if((event.type == ALLEGRO_EVENT_disPLAY_CLOSE))
        {
            break;
        }
    }
    al_destroy_font(font);
    al_destroy_display(disp);
    al_destroy_timer(timer);
    al_destroy_event_queue(queue);
}

Game::Game(WindowConfiguration c)
{
    config = c;
    Start();
}

main.cpp

#include "Engine.hpp"

using namespace Pislify;
int main()
{
    Game g(DefaultwindowConfig());
    return 0;
}

解决方法

由于要在多个源文件中包含Engine.hpp,因此必须将其标记为inline。这样可以多次定义该功能。

namespace Pislify
{
    //...
    inline WindowConfiguration DefaultWindowConfig()
    {
        WindowConfiguration conf;
        conf.WindowWidth = 1280;
        conf.WindowHeight = 720;
        conf.FPS = 60;
        conf.WindowTitle = "game";
    }
    //...

引用https://www.cplusplus.com/articles/2LywvCM9/
5. By marking it as inline,you can put a function definition in a header file (i.e. it can be included in multiple compilation unit,without the linker complaining)