Issue
I have a .so
library and while building it I didn't get any undefined reference errors.
But now I am building an executable using the .so
file and I can see the undefined reference errors during the linking stage as shown below:
xy.so: undefined reference to `MICRO_TO_NANO_ULL'
I referred to this and this but couldn't really understand the dynamic linking.
Also reading from here lead to more confusion:
Dynamic linking is accomplished by placing the name of a sharable library in the executable image. Actual linking with the library routines does not occur until the image is run, when both the executable and the library are placed in memory. An advantage of dynamic linking is that multiple programs can share a single copy of the library.
My questions are:
Doesn't dynamic linking means that when I start the executable using
./executable_name
then if the linker not able to locate the.so
file on which executable depends it should crash?What actually is dynamic linking if all external entity references are resolved while building? Is it some sort of pre-check performed by dynamic linker? Else dynamic linker can make use of
LD_LIBRARY_PATH
to get additional libraries to resolve the undefined symbols.
Solution
Doesn't dynamic linking means that when I start the executable using ./executable_name then if the linker not able to locate the .so file on which executable depends it should crash?
No, linker will exit with "No such file or directory" message.
Imagine it like this:
- Your executable stores somewhere a list of shared libraries it needs.
- Linker, think of it as a normal program.
- Linker opens your executable.
- Linker reads this list. For each file.
- It tries to find this file in linker paths.
- If it finds the file, it "loads" it.
- If it can't find the file, it get's
errno
withNo Such file or directory
fromopen()
call. And then prints a message that it can't find the library and terminates your executable.
- When running the executable, linker dynamically searches for a symbol in shared libraries.
- When it can't find a symbol, it prints some message and the executable teerminates.
You can for example set LD_DEBUG=all
to inspect what linker is doing. You can also inspect your executable under strace
to see all the open
calls.
What actually is dynamic linking if all external entity references are resolved while building?
Dynamic linking is when you run the executable then the linker loads each shared library.
When building, your compiler is kind enough to check for you, that all symbols that you use in your program exist in shared libraries. This is just for safety. You can for example disable this check with ex. --unresolved-symbols=ignore-in-shared-libs
.
Is it some sort of pre-check performed by dynamic linker?
Yes.
Else dynamic linker can make use of LD_LIBRARY_PATH to get additional libraries to resolve the undefined symbols.
LD_LIBRARY_PATH
is just a comma separated list of paths to search for the shared library. Paths in LD_LIBRARY_PATH
are just processed before standard paths. That's all. It doesn't get "additional libraries", it gets additional paths to search for the libraries - libraries stay the same.
Answered By - KamilCuk