Receive “undefined symbol” error when loading library with dlopen

后端 未结 3 1191
梦毁少年i
梦毁少年i 2020-12-13 07:51

I\'m writing some code that uses dynamic shared libraries as plugins.

My command line for building the shared libraries looks like:

cc -shared -fPIC          


        
相关标签:
3条回答
  • 2020-12-13 08:09

    I've found the answer myself.

    I had to add the --export-dynamic flags to the link options for the main executable.

    When creating a dynamically linked executable, add all symbols to the dynamic symbol table. The dynamic symbol table is the set of symbols which are visible from dynamic objects at run time.

    If you do not use this option, the dynamic symbol table will normally contain only those symbols which are referenced by some dynamic object mentioned in the link.

    If you use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself.

    0 讨论(0)
  • 2020-12-13 08:10

    When I encountered the same problem, I just used the following solution. Before loading any plugin, just load the program itself, bringing its symbols to dynamic tables:

    dlopen(NULL,RTLD_NOW|RTLD_GLOBAL);
    

    I think the solution is better. The reason is that, it also solves the same problem if you

    a) your program (or a trird-party module) is linked (not in runtime) against the shared library, which symbols need to be in dynamic table;

    b) can not recompile that module with -rdynamic flag.

    0 讨论(0)
  • 2020-12-13 08:20

    Correct solution is to add -rdynamic to the link command of the main executable. This will add appropriate option to ld (which, when using GNU ld, happens to be --export-dynamic).

    Adding --export-dynamic directly is technically incorrect: it's a linker option, and so should be added as -Wl,--export-dynamic, or -Wl,-E. This is also less portable than -rdynamic (other linkers have an equivalent, but the option itself is different).

    0 讨论(0)
提交回复
热议问题