| View previous topic :: View next topic |
| Author |
Message |
dE_logics Veteran


Joined: 02 Jan 2009 Posts: 1980 Location: $TERM
|
Posted: Thu May 24, 2012 11:28 am Post subject: A few questions on linking concepts. |
|
|
1) To use a library's components, you just have to declare the function/variable in your program?... that's it? Same procedure for static and dynamic libraries?
2) Is manual library loading (in the program) a common practice even now? Or is it that libraries are loaded and unloaded by the OS automatically on demand (while running a single program)? For e.g. if I require the function X which happens to be in libX.so, then will the library be loaded just before the call and unloaded after the call? Can such programs be compiled statically?
3) How is the location of the static library determined?
3.1) At run time (dynamic linker)
3.2) At compile time (static linking)
I know about /etc/ld.so.conf, but then also, does the linker test each library for the target function?
4) In static and dynamic linking (compile time), do we need the source of the linked libraries? If so, where are they placed?
5) There are 2 kinds of dynamic libraries, one is statically aware (library loaded as program starts) and the other is dynamic loading type where you have to manually load and unload libraries in the program in order to use them.
Are these 2 completely different kind of libraries, or is the methods of linking different? e.g. if I want to link my program to libX.so dynamically, then can I link it in a 'statically aware' or 'dynamic loading' way with the same libX.so?
Thanks for clarifying! _________________ Buy from companies supporting opensource -- IBM, Dell, HP, Hitachi, Google etc...
Disfavor companies supporting only Win -- Logitech, Epson, Adobe, Autodesk, Pioneer, Kingston, WD, Yahoo, MSI, XFX
My blog |
|
| Back to top |
|
 |
Genone Retired Dev


Joined: 14 Mar 2003 Posts: 8862 Location: beyond the rim
|
Posted: Thu May 24, 2012 1:31 pm Post subject: Re: A few questions on linking concepts. |
|
|
| dE_logics wrote: | | 1) To use a library's components, you just have to declare the function/variable in your program?... that's it? Same procedure for static and dynamic libraries? |
"Use" is a very vague term. You have to
- declare the signature of an object (like class/function/variable/type) for the compiler to generate appropriate object code
- tell the linker which modules (libraries or object files) should be used to lookup undefined symbols
- provide those modules to the linker (at buildtime) and the runtime linker (for dynamic libraries)
This is just talking about "classic" linking, not dlopen and co (see below).
| Quote: | | 2) Is manual library loading (in the program) a common practice even now? Or is it that libraries are loaded and unloaded by the OS automatically on demand (while running a single program)? For e.g. if I require the function X which happens to be in libX.so, then will the library be loaded just before the call and unloaded after the call? Can such programs be compiled statically? |
Standard procedure is that the dynamic runtime linker (ld-linux.so) loads libraries during program startup, and maps undefined symbols to memory addresses (the latter part can be changed with linker options). What you mean is probably loading via dlopen, see below for that.
| Quote: | 3) How is the location of the static library determined?
3.1) At run time (dynamic linker)
3.2) At compile time (static linking) |
Reread your question for the answer
| Quote: | | I know about /etc/ld.so.conf, but then also, does the linker test each library for the target function? |
No, you tell the linker which libraries to use when you're building your program. Maybe you should go back a step or two ("how do I build a program?") ...
Also see my answer in your other thread about linking vs. loading.
| Quote: | | 4) In static and dynamic linking (compile time), do we need the source of the linked libraries? If so, where are they placed? |
No. You only need the header files and the library archive (.a or .so). Maybe you should try to actually build a simple program ...
| Quote: | 5) There are 2 kinds of dynamic libraries, one is statically aware (library loaded as program starts) and the other is dynamic loading type where you have to manually load and unload libraries in the program in order to use them.
Are these 2 completely different kind of libraries, or is the methods of linking different? e.g. if I want to link my program to libX.so dynamically, then can I link it in a 'statically aware' or 'dynamic loading' way with the same libX.so? |
You're completely screwing up your terminology. Dynamic libraries are always linked dynamically, static libraries are always linked statically.
What you're referring to is loading via dlopen and related functions, usually used to implement plugin interfaces. This is however a topic not really related to linking in the usual sense, and before going into details on this you should understand the basics of building a program ... |
|
| Back to top |
|
 |
wcg Guru

Joined: 06 Jan 2009 Posts: 556
|
|
| Back to top |
|
 |
v_andal Apprentice

Joined: 26 Aug 2008 Posts: 219 Location: Germany
|
Posted: Fri May 25, 2012 7:15 am Post subject: |
|
|
Yep, first of all, one should understand difference between "dynamic linking" and "static linking". The second (static) puts the code of functions into executable, so the functions are always available. The first (dynamic) just marks in which "library" the code of the function can be found. When the program is started, dynamic linker finds all necessary library files and loads necessary functions into memory.
In both cases, there's no need to worry about library files, the linkers (dynamic or static) take care of this.
There's third case though. One can "manually" load code for some function from dynamic library into memory. This is how different plugins are implemented. When the program is started, it may not know which plugins it shall use. At run-time it obtains name of the library file and the name of the function in that file, then dlopen and Co. are used to load necessary code into memory. |
|
| Back to top |
|
 |
wcg Guru

Joined: 06 Jan 2009 Posts: 556
|
Posted: Fri May 25, 2012 9:00 am Post subject: |
|
|
For dynamic linking to code in shared libraries, the whole library
is loaded into memory, and the dynamic linker fixes up references
to static data in the library (errno values, for example) when
the library is loaded, and fixes up references to functions in
the library as they are called. The library will stay in memory
as long as anything linked to it is running. libc.so stays
in memory from when init is started to shutdown if init is
dynamically linked to it.
For the kernel, shared libraries are just another elf executable.
Because it can reload the text (the code) of executables from
persistent storage, under sufficient memory pressure the kernel
will discard unused, then less recently used pages of executables
from memory, reloading them from the executable in storage
if they are accessed by running code. It does this after exhausting
its other resources (installed physical memory, swap, pages
allocated to buffer cache of files that have been loaded, the
directory cache, fragmented pages that can be coalesced
to free up pages of memory, etc; the kernel can jump through
a lot of complex hoops in the time it takes to read a page
from disk, so what seem like extraordinarily complex algorithms
from a coding perspective for freeing pages of memory for
allocation often prove to be "worth it" at runtime). _________________ TIA
Last edited by wcg on Mon May 28, 2012 7:53 pm; edited 1 time in total |
|
| Back to top |
|
 |
dE_logics Veteran


Joined: 02 Jan 2009 Posts: 1980 Location: $TERM
|
Posted: Sat May 26, 2012 3:29 am Post subject: |
|
|
Thanks for the clarification everyone, especially Genone; now things are a lot clearer. _________________ Buy from companies supporting opensource -- IBM, Dell, HP, Hitachi, Google etc...
Disfavor companies supporting only Win -- Logitech, Epson, Adobe, Autodesk, Pioneer, Kingston, WD, Yahoo, MSI, XFX
My blog |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|