Thursday, 15 January 2015

c - Why redefinition of a function which is already present in dynamic or static library does not throw any error? -



c - Why redefinition of a function which is already present in dynamic or static library does not throw any error? -

wy redefinition of function nowadays in dynamic library not throws compilation , linking error?

in below function

#include "calc_mean.h" #include <stdio.h> int mean(int t, int v) { homecoming 0; } int main () { int themean = mean(3,6); printf("\n %d\n",themean); }

inside shared library definition of mean function nowadays below.

#include <stdio.h> #include "calc_mean.h" int mean(int a, int b) { homecoming (a+b)/2; }

the definition of mean function nowadays in shared library libmean.so. during compilation don't see redefinition error , compilation successful.

and on successful execution o/p see 0 instead of 4 function definition of mean within shared library not getting executed 1 within main module getting executed.

why happening so?

the linker links in function library if function had not yet been found during compilation/linking process.

the reason difference in functionality there different types of symbols. library function weak symbol. included if not defined. nm tool listing symbols in object or executable. in it's manpage, can find list of types of symbols.

there wikipedia page on weak symbols.

c linker-error duplicate-symbol

No comments:

Post a Comment