sundq
sundq

Reputation: 763

throw undefined symbol when load dynamic lib with dlopen

I am having main program which has one base class that needs to be used by shared library. Base class has some pure virtual method that derived class in shared library needs to be over written. Main program load shared library using dlopen system call. dlopen("shared file name", RTLD_NOW|RTLD_GLOBAL);

Base class

class RateComputer
{
  public:
    RateComputer();
    virtual ~RateComputer();
    virtual void OnMarketData() = default;
  private:
  };

Derived class in shared library.

  class WeightedRateComputer : public RateComputer
  {
  public:
    WeightedRateComputer();
    ~WeightedRateComputer();
    void OnMarketData() override;
  private:
};

and implement

WeightedRateComputer::WeightedRateComputer()
{
    printf("in Construct\n");
}

void WeightedRateComputer::OnMarketData()
{
    printf("in OnMarketData\n");
}


extern "C" WeightedRateComputer* createObj()
{
    return new WeightedRateComputer();
}

While compilation of binary and so file I have added -rdynamic flag. But during loading of library with dlopen it gives error "undefined symbol: _ZTI12RateComputer".

int main()
{
    void *handle = dlopen("../xx.so", RTLD_LAZY |RTLD_GLOBAL);
    if (handle == NULL)
    {
        printf("%s\n", dlerror()); //throw error here
        return 0;
    }   
    return 0;
}

Upvotes: 0

Views: 921

Answers (1)

Theforgotten
Theforgotten

Reputation: 140

As Constructor and Destructor are declared in your RateComputer class, they need to be defined.

At least, you can use default c++11 keyword to use default implementation. Moreover, as your RateComputer class is an interface, your OnMarketData method should be pure virtual, hence only declared in the interface and implemented in derived classes.

Here's the RateComputer class code I end up with :

class RateComputer
{
  public:
    RateComputer() = default;
    virtual ~RateComputer() = default;
    virtual void OnMarketData() = 0;

};

Hope this helps.

Upvotes: 1

Related Questions