I have two libraries, named a.lib and b.lib. Both libraries have a function char test(void), but return different characters.
a.c (a.lib)
char test(void);char test(void) { return 'a';}
b.c (b.lib)
char test(void);char test(void) { return 'b';}
I create a project link these two libraries and call test() in main() function.
main.c
char test(void);int main(){ char c = test(); return 0; }
I expected it should be "Error: L6200E: Symbol multiply defined" error, but " 0 errors".
Why is there no link error?
Thanks for the reply.
Because it is a library not an object?
The linker pulls from the first library it can achieve closure with, that's why the ordering of libraries is important.
The library can contain multiple objects, if pulling in your symbol draws in a larger object with other symbols, these may conflict in your namespace resulting in a multiply defined error.
This is also why the compiler has option to create a new object for each function.
A book on linker design may explain this better.
Thanks for your reply.
Do I have any chance to find out the same function name in two or more libraries?
Westonsupermare Pier said:The linker pulls from the first library it can achieve closure with
Or, if there is a matching definition from your source code, it will use that.
Thus you can override library functions in your source code.
clliao.tw said:Do I have any chance to find out the same function name in two or more libraries?
What do you mean by that?
I hope to get error message when two or more libraries have the same-name function.
Why would you get an error message?
As Westonsupermare Pier explained, it is not an error condition!
I wanted to use the function in a.lib and I didn't know b.lib has such a same-name API at first, so I got a wrong result. After checking map file, I found the linker connect to the wrong lib (b.lib). My purpose is to avoid the same situation. If linker can not help this, is there any other method to help? Thanks.
Are these your own libraries, or from 3rd parties?
Usually, 3rd-party libraries will give their symbols a distinctive prefix for exactly this reason ...
These are our own libraries, but from different departments. I can ask them to have functions with different prefix but I also need a solution for double check.
clliao.tw said:These are our own libraries
Then this really is a fault in the libraries - for the very reasons you are finding!
clliao.tw said:I can ask them to have functions with different prefix
Yes - they need to do that!
They should have done that in the first place - so it seems some education is needed in those departments!
Maybe point them to this thread?
Thanks for your reply. I'll strongly recommend them following the naming rule. I'll also ask them to provide all the object files so that I can double check this issue by linking all the object files.
clliao.tw said: I'll also ask them to provide all the object files so that I can double check
Why would you do that at the Object level?
Surely, it'd be easier at the source level?