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.