-1

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?

Dov
  • 8,000
  • 8
  • 46
  • 75
  • 2
    Try to explain _doesn't work_ What are the symptoms? Is there an error message? If `dlopen` returns NULL, call `dlerror` for error-message. (Note: if your shared library is in C++, then the main program must be C++ too, not C) – Lorinczy Zsigmond Jun 01 '18 at 03:45
  • 2
    Try printing return value of `dlerror` or run program with `export LD_DEBUG=all`. – yugr Jun 01 '18 at 05:55
  • @Lorinczy was first, if you care to make it an answer i will mark it correct. – Dov Jun 03 '18 at 02:42
  • (Thanks, but it's not necessary, it's okay as it is.) – Lorinczy Zsigmond Jun 03 '18 at 08:30

1 Answers1

2

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362