首页 > 解决方案 > Allgro 5中的定义错误'...的多重定义首先在这里定义...'

问题描述

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

我想制作一种类似结构的引擎,所以我制作了一个应用程序类,以便更轻松地为 ludum 敢于制作游戏

因此您将创建一个类并使其继承自 app 并填写 update init 和 draw 方法

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

我的构建脚本看起来像这样

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

希望我能得到答案

/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

引擎.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();
}

主文件

#include "Engine.hpp"

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

标签: c++compiler-errorsallegro

解决方案


由于您包含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)


推荐阅读