- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Why does -r option (relocatable) make ld not find any libraries
In world of programming and computer science, there are different techniques and tools used to solve various problems. One of common problems that programmers encounter is linking errors. When a program is compiled and linked, it has to find necessary libraries to function. However, there are times when linker fails to find required libraries, resulting in a linking error. In this article, we will discuss why -r option (relocatable) makes ld not find any libraries and provide examples.
What is -r option?
The -r option is a command-line option used with GNU linker, ld. option specifies that output of linker should be a relocatable object file, which is a file that can be further linked to create an executable or shared object file. -r option is typically used when linking object files to create a static library. When -r option is used, linker does not resolve all undefined symbols in object files, and output is a relocatable object file that can be used later for further linking.
Why does -r option make ld not find any libraries?
When -r option is used, ld does not look for any libraries because it assumes that all necessary symbols are defined in input object files. This means that if there are any undefined symbols, ld will not be able to find them in any libraries, resulting in a linking error.
To better understand why -r option makes ld not find any libraries, let us consider an example. Suppose we have two object files, a.o and b.o, that we want to link to create a static library. two object files contain some undefined symbols that need to be resolved.
If we use following command to link object files −
$ ld -r a.o b.o -o libab.o
The output will be a relocatable object file, libab.o, that can be used later for further linking. However, if we try to link libab.o to create an executable file, we will get a linking error because undefined symbols in object files were not resolved.
$ ld libab.o -o myprog
Undefined symbols: _foo _bar
In this case, ld did not find any libraries containing symbols _foo and _bar, resulting in a linking error.
To resolve linking error caused by -r option, we need to link necessary libraries explicitly. This means that we need to use -l option to specify libraries that contain undefined symbols.
For example, if undefined symbols in our object files are defined in libmylib.a library, we can link our object files and library as follows −
$ ld -r a.o b.o -lmylib -o libab.o
This will link object files and library to create a relocatable object file, libab.o, that can be used later for further linking.
The -r option is useful when creating a static library because it allows us to create an intermediate object file that can be used to create multiple static libraries. This saves time and reduces redundancy because we do not have to recompile object files every time we want to create a static library.
The -r option can also be used to create a relocatable shared library. A shared library is a library that is loaded into memory at runtime and can be shared by multiple programs. When creating a shared library, we can use -r option to create an intermediate relocatable object file that can be linked with other object files and shared libraries to create a shared library.
It is important to note that -r option does not prevent ld from searching for libraries when creating an executable or shared library. option only affects behavior of linker when creating a relocatable object file.
In conclusion, -r option makes ld not find any libraries because it assumes that all necessary symbols are defined in input object files. This can result in a linking error if there are any undefined symbols that are not defined in input files. To resolve linking error, we need to link necessary libraries explicitly using -l option. Understanding how -r option works and how to resolve linking errors caused by option is important for programmers who want to create static libraries and executables.
- Related Articles
- Why does our urine does not have any blood cell?
- Why accessing an array out of bounds does not give any error in C++?
- Why does the order in which libraries are linked sometimes cause errors in GCC?
- Why does Python Not Support Multithreading?
- Why scale_fill_manual does not fill the bar with colors created by using ggplot2 in R?
- In the famous Monalisa painting by Leonardo Da Vinci, why does the subject not have any eyebrows?
- Why does ball not float in water?
- Why does clinical thermometer not have alcohol ?
- Why ship does not sink in sea?
- A fused bulb does not glow. Why?
- Why does Youtube use TCP not UDP?
- Why does distilled water not conduct electricity, whereas rainwater does?
- Why C++ does not have a virtual constructor?
- PHP Why does this && not trigger as false?