dlopen vs linking overhead

前端 未结 1 747
我寻月下人不归
我寻月下人不归 2021-02-04 19:09

Suppose I have a library - foo.so . When building my binary (which needs this library), I can either (1) link foo.so , or, (2) within the program source code, dlopen this libra

相关标签:
1条回答
  • 2021-02-04 19:55

    If the library is a shared object (ie some lib*.so file) compiled with gcc -Wall -fPIC -O2 and linked with gcc -shared then it is an ELF Position Independent Code shared library.

    PIC is a bit more costly on 32 bits x86 -which has few registers- than on 64 bits x86-64 -which has some addressing mode facilitating PIC

    It is the same (in steady state) performance wise if it is dlopen-ed or if it is dynamically linked. Because in both cases the real linking is done by the dynamic linker (e.g. ld-linux.so) sine libdl.so is basically a wrapper to the dynamic linker.

    What matters performance wise when called is the code inside the lib*.so and it does not change if you dlopen it or if you link it.

    Things could be slightly different if the library is statically linked lib*.a. You could even compile and link both the library and the program with the link time optimization ability of recent GCC compilers (compile and link with gcc -flto -Wall -O2)

    Read Drepper's How to Write Shared Library paper and the Program Library HowTo and Levine's Linkers & Loaders book.

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