This is powerful trick that allows the linker to

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: ned twice (Rule 1): 7.6. SYMBOL RESOLUTION 1 2 3 4 5 6 7 359 1 2 3 4 5 6 /* foo2.c */ int x = 15213; int main() { return 0; } /* bar2.c */ int x = 15213; void f() { } However, if x is uninitialized in one module, then the linker will quietly choose the strong symbol defined in the other (Rule 2): 1 2 3 4 5 6 7 8 9 10 11 12 /* foo3.c */ #include <stdio.h> void f(void); int x = 15213; int main() { f(); printf("x = %d\n", x); return 0; } 1 2 3 4 5 6 7 /* bar3.c */ int x; void f() { x = 15212; } At run time, function f changes the value of x from ½ ¾½¿ to ½ ¾½¾, which might come as a unwelcome surprise to the author of function main! Notice that the linker normally gives no indication that it has detected multiple definitions of x: unix> gcc -o foobar3 foo3.c bar3.c unix> ./foobar3 x = 15212 The same thing can happen if there are two weak definitions of x (Rule 3): 1 2 3 4 5 6 7 8 9 10 11 12 13 /* foo4.c */ #include <stdio.h> void f(void); int x; int main() { x = 15213; f(); printf...
View Full Document

Ask a homework question - tutors are online