Linkers, Loaders & Executables

When linking with a static library (.a file), what does the linker put into the executable?

  1. AAll object files in the library
  2. BOnly the library's file name, with no code
  3. COnly the object files in the library that are needed to resolve undefined references
Show answer and explanation

Answer: C. Only the object files in the library that are needed to resolve undefined references

A static library is an archive bundling many object files. The linker extracts and includes only those object files that define symbols still lacking a definition. So even with a library containing many functions, such as the standard library, the executable grows only by the functions actually used.

  • A. Including everything would bloat the executable.
  • B. Recording only names is dynamic linking.
  • C. Correct. Unused parts are left out.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. With the GNU linker, writing a static library before the object file that uses it, as in "gcc -lmylib main.o", can cause undefined-reference errors. Why?
  2. Which describes position-independent code (PIC)?
  3. What is the role of the GOT (global offset table) used in shared libraries?
  4. When dynamic linking uses lazy binding through the PLT (procedure linkage table), which statement is correct?
  5. What is the role of the dynamic linker (such as ld.so) when a dynamically linked program starts on Linux?