Search code examples
libstdc++dlopendlsym

dlopen fails in a library instantiating an object


We have a minimal working example of dlopen that works.

void* lib = dlopen("servlets.so", RTLD_NOW);
void* p = dlsym(lib, "getServlets");

However, if we add another function to the shared library (not even if it is called) then the library does not work (even though the code is never called)

uint32_t count = 0;
Servlet** servlets;
extern "C" {
  void generate() {
     servlets = new Servlet*[3];
     servlets[0] = new Servlet(...);
  }
  Servlet** getServlets() { return servlets; }
  uint32_t getServletNum() { return count; }
}

This must be because the code in the shared object is referencing some symbol that we don't have, but we do not know what. The code compiles and links without a problem.

  1. Is there any way to find out what the error is? No error is reported, except that the library pointer returns NULL and the library does not load.

  2. How do we link to a library so that dlopen works?


Solution

  • No error is reported, except that the library pointer returns NULL

    A library pointer can't return anything. You probably mean dlopen() returns NULL.

    If that's what you mean, that is the error being reported. If you want to know more about why the error was returned, use dlerror() to find out.