Compile C program using dlopen and dlsym with -fPIC

前端 未结 3 893
-上瘾入骨i
-上瘾入骨i 2021-01-05 19:21

I am having a problem about a wrong symbol resolution. My main program loads a shared library with dlopen and a symbol from it with dlsym. Both the program and the library a

相关标签:
3条回答
  • 2021-01-05 20:07

    It seems this issue can take place in one more case (like for me). I have a program and a couple of a dynamically linked libs. And when I tried to add one more I used a function from a static lib (my too) in it. And I forgot to add to linkage list this static lib. Linker was not warn me about this, but program was crushing with segmentation fault error.

    Maybe this will help for someone.

    0 讨论(0)
  • 2021-01-05 20:17

    FWIW, I ran into a similar problem when compiling as C++ and forgetting about name mangling. A solution there is to use extern "C".

    0 讨论(0)
  • 2021-01-05 20:23

    I suspect that there is a clash between two global symbols. One solution is to declare a in the main program as static. Alternatively, the linux manpage mentions RTLD_DEEPBIND flag, a linux-only extension, which you can pass to dlopen and which will cause library to prefer its own symbols over global symbols.

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