Why does -r option (relocatable) make ld not find any libraries


Introduction

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.

Example

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.

Solution

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.

Additional Information

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.

Conclusion

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.

Updated on: 03-Mar-2023

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements