0

I want to compile a program into a shared library, but im getting an undefined symbol error when I use it if one of the objects used at compile time has inheritance. I have simplified it so that it will be more understandable, but basically it looks like this:

api.hpp:

    #ifndef API_H
    #define API_H

    //std libraries
    #include <deque>
    #include <memory>

    #include "simulations/simulationA.hpp"

    class Api {


      public:
        Api();
        ~Api();


      private:
        std::deque < std::shared_ptr<Simulation> > _simulations_queue;

    };

    #endif

simulation.hpp:

    #ifndef SIMULATION_H
    #define SIMULATION_H

    class Simulation {


      public:
        Simulation(int simulation_id);
        ~Simulation();

        virtual void run();

    protected:
        int _simulation_id;

    };

    #endif

simulation.cpp

    #include "simulation.hpp"

    Simulation::Simulation(int simulation_id)
    {
        _simulation_id = simulation_id;
    }

    Simulation::~Simulation()
    {
    }

simulationA.hpp

    #ifndef SIMULATIONA_H
    #define SIMULATIONA_H

    //std libraries
    #include <string>
    #include <atomic>
    #include <unordered_map>

    #include "simulation.hpp"


    class SimulationA : public Simulation{


      public:
        SimulationA(int simulation_id);
        ~SimulationA();

        void run();

    };

    #endif

simulationA.cpp

#include "simulationA.hpp"

    SimulationA::SimulationA(int simulation_id) : Simulation(simulation_id)
    {

    }

    SimulationA::~SimulationA()
    {

    }

    void SimulationA::run()
    {

    }

Originally, to compile it I did the following:

g++ -c -fpic api.cpp -o api.o
g++ -c -fpic simulation.cpp -o simulation.o
g++ -c -fpic simulationA.cpp -o simulationA.o 

g++ -shared -o shared_api.so api.o simulationA.o simulation.o 

It compiles fine, but when I create an api instance object, it throws the following error:

undefined symbol _ZTI10Simulation

This only happens because of the inheritance. If I remove it it doesnt give an error. Why does that happen? I have tried to change the linking order, to compile it with the -Wl,--start-group -Wl,--end-group , but none have worked so far.

gmm
  • 943
  • 1
  • 17
  • 30
  • It could be, but I really cant find the mistake I made. The most obvious one should be the linking order, but I believe i have made it right. (And i have tried changing it, to no avail) – gmm Aug 17 '15 at 17:28
  • Yeah you are right, is a duplicate. Should I delete the question? Answer is here: Virtual methods must either be implemented or defined as pure. I was implementing it (still not sure why it didnt work), but when I added virtual void run() = 0; (notice the = 0) it compiles and links correcly – gmm Aug 17 '15 at 17:46

0 Answers0