Linking from this listing we see that the call

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , if we try to compile and link the following source file on a Linux machine, 1 2 3 4 5 6 void foo(void); int main() { foo(); return 0; } then the compiler runs without a hitch, but the linker terminates when it cannot resolve the reference to foo: unix> gcc -Wall -O2 -o linkerror linkerror.c 358 /tmp/ccSz5uti.o: In function ‘main’: /tmp/ccSz5uti.o(.text+0x7): undefined reference to ‘foo’ collect2: ld returned 1 exit status CHAPTER 7. LINKING Symbol resolution for global symbols is also tricky because the same symbol might be defined by multiple object files. In this case, the linker must either flag an error, or somehow chose one of the definitions and discard the rest. The approach adopted by Unix systems involves cooperation between the compiler, assembler, and linker, and can introduce some baffling bugs to the unwary programmer. 7.6.1 How Linkers Resolve Multiply-Defined Global Symbols At compile time, the compiler exports each global symbol to the assembler as either strong or weak, and the assembler encodes this information implicitly in the symbol table of the relocatable object file. Functions and initialized global variables get...
View Full Document

Ask a homework question - tutors are online