In said:
Are you sure? Let's try:
fangorn:~/tmp 377> cat lib1.c
#include <stdio.h>
void functA(void) { puts("I'm functA from lib1"); }
fangorn:~/tmp 378> gcc -c lib1.c
fangorn:~/tmp 379> ar -r lib1.a lib1.o
fangorn:~/tmp 380> cat lib2.c
#include <stdio.h>
void functA(void) { puts("I'm functA from lib2"); }
fangorn:~/tmp 381> gcc -c lib2.c
fangorn:~/tmp 382> ar -r lib2.a lib2.o
fangorn:~/tmp 383> cat test.c
void functA(void);
int main()
{
functA();
return 0;
}
fangorn:~/tmp 384> gcc test.c lib1.a lib2.a
fangorn:~/tmp 385> ./a.out
I'm functA from lib1
fangorn:~/tmp 386> gcc test.c lib2.a lib1.a
fangorn:~/tmp 387> ./a.out
I'm functA from lib2
So, the right answer is: it depends on your implementation. Mine was
certainly happy with two libraries defining the same function.
It's most likely the linker that complains, not the compiler.
No one complained on my system. Should I ask for a refund? ;-)
You can't. There are ways around it, but not for the feint-hearted.
My example above clearly shows that you can and even the faint of heart
can do it. So, the right answer is, again: it depends on your
implementation.
By far the easiest way is to call your functions different names.
You can also declare them as static and hide them behind structures,
but why?
The usual reason is that you have to use two libraries defining the same
function, without having control over the source code of any of them.
If the linker is less tolerant than the typical Unix linker, there may be
no solution to this problem, short of editing the binary of one of the
libraries, to alter the function name.
Dan