Post by James K. Lowden
On Sun, 14 Feb 2021 20:35:06 -0000 (UTC)
Post by Kaz Kylheku Post by James K. Lowden
If it doesn't work that way, how is the linker supposed to choose
between two implementations of a function defined in two
"error: multiply defined symbol foo", I would hope.
No, of course not. The One Definition Rule applies to data, not
You're not suggesting, are you, that I can't write my own memcpy, put
it in memcpy.a, and link it into my C program, overriding the function
defined in libc?
One of us doesn't understand the other. I prepared for that person to
be me, but right now I'm baffled at the discussion.
The following citations are from the C2011 standard:
"An external definition is an external declaration that is also a
definition of a function (other than an inline definition) or an object.
If an identifier declared with external linkage is used in an expression
(other than as part of the operand of a sizeof or _Alignof operator
whose result is an integer constant), somewhere in the entire
program there shall be exactly one external definition for the
identifier; otherwise, there shall be no more than one." (6.9p5).
Notice that the rules are the same whether the identifier identifies a
function or an object.
"If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a
constraint or runtime-constraint is violated, the behavior is
"All identifiers with external linkage in any of the following
subclauses (including the future library directions) and errno are
always reserved for use as identifiers with external linkage." (7.1.3p1)
Note that memcpy one of the identifiers with external linkage in the
"If the program declares or defines an identifier in a context in which
it is reserved (other than as allowed by 7.1.4) ... the behavior is
Providing a definition for memcpy with external linkage is not one of
the cases allowed by 7.1.4.
It's quite common for implementations to define the behavior when
multiple definitions are available, in a way that's convenient. The
behavior you describe is one example of that. However, they are not
required to do so.