0
votes

I'm trying to build a shared library that another program will load at runtime, but I'm getting an undefined symbol error when I call dlopen() on the library. The missing symbol is a function (call it foo_func) defined in the main project where my program is built (and used in the main program as well), and I'm including its header in the shared library definition.

Based on other posts on stackoverflow, I've tried using the -fPIC and -shared flags to build my library, and -rdynamic -ldl to build my main program, but still get the error. The library roughly looks like this. Any ideas what could be wrong?

#include <header where foo_func is defined>
class foo {
  public:
    foo() {x=0;}
    virtual void init(){};
    double x;  
};
class bar: public foo{
  public:
    void init() {
        foo_func(...)
    }
};
extern "C" foo* create() { return new bar; }
1
Use nm | less to look at the symbols that ARE defined in your .so shared library. If you find "foo_func()", then your client is calling it incorrectly. Perhaps you're missing an "extern C", perhaps you're calling it as a standalone function instead of a class member - there are lots of possibilities. Start with "nm", and work from there. - paulsm4
Hard to tell given <header where foo_func is defined> is not something anyone could ever try and compile. Build an MVCE please. - n. 1.8e9-where's-my-share m.
Please edit your question to give the actual symbols involved and explain in which files they are declared and defined and give their exact declarations & definitions. Give the exact messages, and the value of dlerror() when something is failing - Basile Starynkevitch

1 Answers

0
votes

The error is correct. When the dlopen() call is made, it cannot return a successful result unless it can locate every symbol referenced. And because that one symbol is in your main program, not a library, the dlopen() call will be unable to load your main pgm, therefore unable to find the definition of that symbol.

The design of having a shared library making use of code dedicated to your main program is a fundamental problem, and no build flag can fix that. My recommendation is to re-think that design -- there must be a better way.