On Apr 11, 11:02 pm, "Richard" <rksa...@gmail.comwrote:
I agree fully with Larry about #1. I believe it to be legal to have
multiple #defines of "open", but it does boil down to text
replacement. In my case, I want all usages of "open" to resolve to
the same function, so this option is out of the question.
#2, seems to do the trick, though I'm not sure if it's because the
linker is lenient. In one library a function is defined in 'extern
"C"' and declared. In another library a function with the same name
and signature is defined the same way. When I link these libraries
into an executable, either of the functions ends up being used based
on what the linking order was!
Any ideas why the linker doesn't complain there are "multiply defined
symbols"?
Because you didn't include both object files in the program?
Symbols aren't in libraries, they are in object files (the
results of a compilation). A library is a collection of object
files, which the linker conditionally includes in your program:
if the object file defines a symbol which corresponds to an
unresolved external, the linker pulls it out of the library, and
adds it to your program. If it doesn't, the linker just ignores
it.
I tried defining a function in this manner twice in the
same file, and the compiler catches this. But when they are in
different libraries, it gets past both the compiler and the linker.
That's more or less the definition of a library. You can still
get multiple definitions, however: objA.o defines a and c,
objB.o defines b and c. Put them in a library, and specify it
when linking a program with undefined symbols a and b.
The important thing to remember when using libraries is that
they are only sets of object files, and that the inclusion of
the object file is conditional. If you have object files that
you absolutely want in your program, you can't put them in a
library; you have to specify them explicitly, as object files.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34