Linkers, Loaders & Executables
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?
- AThe linker works left to right and does not extract functions not yet referenced when it reads the library
- BOnly one library may be written, and only at the end of the command
- CWriting the library first erases its contents
Show answer and explanation
Answer: A. The linker works left to right and does not extract functions not yet referenced when it reads the library
The GNU linker processes files in command-line order, and when it meets a static library it extracts only the objects that resolve references still undefined at that moment. If the library comes first, main.o's references do not exist yet, so nothing is extracted, and the linker does not go back after reading main.o. That is why libraries are normally written after the files that use them.
- A. Correct. The order matters.
- B. Several libraries may be given.
- C. The library's contents are never erased.