0

i try to compile my allegro 5 program but i get this for some reason

i want to make sort of an engine like structure so i made an app class to make it easier to make game for like ludum dare

so you will make a class and make it inherit from app and just fill out the update init and draw methods

i am really a beginner in compiling from the terminal (i use linux)

my build script looks like this

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

hope i get an answer

/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

my code : 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;
}
Pislify
  • 1
  • 1

1 Answers1

0

Since you're including Engine.hpp in multiple source files, you have to mark it as inline. Doing so will allow the function to be defined multiple times.

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

Quote from 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)

rdbo
  • 144
  • 10