Why symbols of a shared library are not resolved at link time?

前端 未结 6 1101
孤独总比滥情好
孤独总比滥情好 2020-12-19 04:58

This is my 2nd post on this site in my effort to understand the compilation/linking process with gcc. When I try to make an executable, symbols need to be resolved at link t

相关标签:
6条回答
  • 2020-12-19 05:13

    A executable requires a entry point. But a shared library can be built without the entry point and later the executable can be compiled with this shared library.

    0 讨论(0)
  • 2020-12-19 05:24

    I think the linker option -Bsymbolic is what you're looking for.

    0 讨论(0)
  • 2020-12-19 05:25

    Since you didn't give the -c (compile only) option, you requested gcc to compile the two source files and link them with the standard library (libc) and the c run-time startup (crt0, typically) to produce a running program. crt0 tries to enter your program by calling main(), which is the undefined symbol the linker can't find. It can't find it because you don't have a main() in either of your .c files, right?

    So, on to your actual question, "Why symbols of a shared library are not resolved at link time?" The answer is, what do you mean by "link time?" By defintion, a dynamically linked program isn't "linked" until it starts (or maybe not even then, depending on your system.)

    On a Linux system, you can see which dynamic libraries a program depends on with the ldd command (on Mac OS use 'otool -L'). The output of ldd will tell you which dynamic libraries a program depends on, which where found in the library search path, and which ones cannot be found (if any).

    When you dynamic program starts, the dynamic linker that was linked into it locates and loads the dynamic libraries the program depends on, and "fixes" the references to the external symbols. If any of these fail, your program will fail to start. One all of the formerly unresolved symbols have been resolved, the dynamic linker returns and the C runtime will call your main() function. (It's somewhat different on Mac OS, but similar in effect, the linking happens after your program is started.)

    0 讨论(0)
  • 2020-12-19 05:32

    The linked has no way of knowing, in ELF at least, where the symbols are (i.e. in which libraries). In OS X, on the other hand, you need to link libraries the way you described. In the end, it is a question of design. One is more flexible, the other, more rigorous.

    0 讨论(0)
  • 2020-12-19 05:33

    Even when you build a shared library it must resolve all the dependencies.

    Thus when a shared library is loaded at compile time it knows what other shared libraries to load at runtime so that it can resolve other dependencies.

    1) Build a shared (look.<sharedLib>) library with look()
    2) Build a shared (hg.<sharedLib>) library with hello() bye() link against look.<sharedLib>
    3) Build Application with main() that links against hg.<sharedlib>

    At runtime the application will then load hg.<sharedlib> which will intern load the shared library look.<sharedlib>

    0 讨论(0)
  • 2020-12-19 05:35

    Does adding -z defs when building the library do what you want? If not, check the ld man pages, there are quite a few options on the handling of undefined symbols.

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