C compilers' behaviour for multiple function definition

R

RealCat

If there are functions whose names are the same in object files and
library files, what should happen during the compliation? Should this
always cause a link error, or can C compilers make assumptions such
as : "function definition in the object file has higher precedence
over the one in the library file" or "the one in the previously linked
library file has higher precedence over the ones in the library files
linked later."?

Is there any standard behaviour for this case or does it vary with
compiler implimentations?
 
S

Sjouke Burry

RealCat said:
If there are functions whose names are the same in object files and
library files, what should happen during the compliation? Should this
always cause a link error, or can C compilers make assumptions such
as : "function definition in the object file has higher precedence
over the one in the library file" or "the one in the previously linked
library file has higher precedence over the ones in the library files
linked later."?

Is there any standard behaviour for this case or does it vary with
compiler implimentations?
The compilers/linkers I have used, always used
supplied code first, and after that search the
libraries for the rest.
 
D

dj3vande

If there are functions whose names are the same in object files and
library files, what should happen during the compliation? [...]
Is there any standard behaviour for this case or does it vary with
compiler implimentations?

I wouldn't be surprised if POSIX or SuS standardizes it, if that's good
enough for you. C doesn't (partly because it doesn't have the concept
of "object files" or "libraries").

N1256 6.9#5 requires that there be at most one (either exactly one or
zero-or-one, depending on whether it's used or not) external definition
of a symbol "somewhere in the entire program"; this is not in a section
labeled "constraints", so violating it invokes undefined behavior, but
it's up to implementations to define whether the library is part of
"the entire program" (if it isn't, you're not violating this requirement,
and even if it is, this is the sort of undefined-by-standard-C behavior
that it's reasonable and useful for an implementation to define).

I would expect most linkers to not look in libraries for symbols that
are defined in object files, but check your documentation to be sure.


dave
 
J

James Kuyper

RealCat said:
If there are functions whose names are the same in object files and
library files, what should happen during the compliation? Should this
always cause a link error, or can C compilers make assumptions such
as : "function definition in the object file has higher precedence
over the one in the library file" or "the one in the previously linked
library file has higher precedence over the ones in the library files
linked later."?

Is there any standard behaviour for this case or does it vary with
compiler implimentations?

The standard does not address this issue at all, leaving it as
implicitly undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top