multiple function definitions

S

subramanian100in

Suppose I have

#include <stdio.h>
#include <string.h>

size_t strlen(const char *str)
{
printf("from strlen - %s\n", str);

return 0; // return some dummy value
}

int main(void)
{
printf("%zu\n", strlen("test message"));
return 0;
}

When I compile this program under Redhat Linux with the command
gcc -std=c99 -pedantic -Wall -Wextra x.c

There is no linker error which I expected because of multiple strlen()
definitions - one in this file and the other in the standard library.
Am I wrong ?

However if I have another file say y.c containing the another
definition for strlen(), and compile
gcc -std=c99 -pedantic -Wall -Wextra x.c y.c

I get linker error for multiple definitions for strlen().

I am unable to understand the difference. Kindly explain.

Thanks
 
F

Fred Kleinschmidt

Suppose I have

#include <stdio.h>
#include <string.h>

size_t strlen(const char *str)
{
printf("from strlen - %s\n", str);

return 0; // return some dummy value
}

int main(void)
{
printf("%zu\n", strlen("test message"));
return 0;
}

When I compile this program under Redhat Linux with the command
gcc -std=c99 -pedantic -Wall -Wextra x.c

There is no linker error which I expected because of multiple strlen()
definitions - one in this file and the other in the standard library.
Am I wrong ?

However if I have another file say y.c containing the another
definition for strlen(), and compile
gcc -std=c99 -pedantic -Wall -Wextra x.c y.c

I get linker error for multiple definitions for strlen().

I am unable to understand the difference. Kindly explain.

The linker will look in libraries only for modules that have not been
found in the object files being linked. Since your object code
contains a module strlen, it uses that one and does not try to
the to extract it from the library.

In the second case, you have it in two object modules. The linker
is loading everything in the object modules (they are not libraries).
 
R

Richard Heathfield

(e-mail address removed), India said:

There is no linker error which I expected because of multiple strlen()
definitions - one in this file and the other in the standard library.
Am I wrong ?

The Standard doesn't give you licence to invade implementation
namespace. If you do so, it doesn't define what will happen.

In this case, however, it's pretty clear what's going on - the linker is
resolving function calls by linking in the object code that you wrote,
and it only goes to the library when it can't resolve references
"locally". That's legal behaviour.
 
J

Jack Klein

(e-mail address removed), India said:



The Standard doesn't give you licence to invade implementation
namespace. If you do so, it doesn't define what will happen.

In this case, however, it's pretty clear what's going on - the linker is
resolving function calls by linking in the object code that you wrote,
and it only goes to the library when it can't resolve references
"locally". That's legal behaviour.

No, it is not. You over snipped, but the OP defined his own function
with the name "strlen" at file scope with external linkage. The names
of all standard library functions are reserved for use as identifiers
with external linkage regardless of whether the corresponding header
file is included.

So the program has undefined behavior, no diagnostic required.

Since he included <string.h>, the name is also reserved at file scope
for any usage. So the static keyword would not remove the undefined
behavior.

On the other hand, he could legally name his own function "strlen" if
he gave it internal linkage with the static keyword and he did not
include <string.h>.
 
J

Jack Klein

Suppose I have

#include <stdio.h>
#include <string.h>

size_t strlen(const char *str)
{
printf("from strlen - %s\n", str);

return 0; // return some dummy value
}

int main(void)
{
printf("%zu\n", strlen("test message"));
return 0;
}

When I compile this program under Redhat Linux with the command
gcc -std=c99 -pedantic -Wall -Wextra x.c

There is no linker error which I expected because of multiple strlen()
definitions - one in this file and the other in the standard library.
Am I wrong ?

You are breaking the rules of the language and causing undefined
behavior, which is something that the compiler is not required to
diagnose and is not required to work or fail to work in any particular
way.

In this case, all identifiers beginning with "str" followed by a lower
case letter with external linkage are reserved, you are not allowed to
use them in your program.

Even if you used the static keyword on the function definition to give
it internal linkage, you are breaking the rules because all
identifiers beginning with "str" followed by a lower case letter are
reserved at file scope regardless of linkage if you include
However if I have another file say y.c containing the another
definition for strlen(), and compile
gcc -std=c99 -pedantic -Wall -Wextra x.c y.c

I get linker error for multiple definitions for strlen().

I am unable to understand the difference. Kindly explain.

Apparently you are trying to cause your compiler to generate
diagnostic messages for some reason. The problem is that the errors
you are putting into your sample programs fall into the category of
undefined behavior, which the compiler is not required to recognize or
diagnose.

What are you hoping to accomplish be generating diagnostic messages?
In general, it is better to expend the effort to eliminate them.
 
R

Richard Heathfield

Jack Klein said:
No, it is not.

Yes, it is.
You over snipped,

I don't think so. On the other hand, I wasn't expecting the Spanish
Inquisition over such a simple question.
but the OP defined his own function
with the name "strlen" at file scope with external linkage.

Yes, he did. And that, ***as I pointed out*** in the very text you
quoted above, is against the rules of C. The behaviour is undefined.
Therefore, the linker can do what it likes. And it did. Maybe I didn't
say it very well, but I did say it.
 
A

Arthur J. O'Dwyer


For Jack, and anyone else with links to that document:
That mirror's new address on the Web is the strikingly similar,
yet fundamentally distinct,

http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Please update your bookmarks accordingly. :)

Astute readers may have noticed that I've been posting here a
lot less frequently of late, and when I do, my return address has
been at "alumni.cmu.edu" instead of "andrew.cmu.edu". Well, my
CMU student network access is about to run out for good, so if I
should drop off Usenet entirely, you'll know the reason is my lack
of a sufficiently hassle-free newsreader outside of CMU. All my
old Web stuff is in the same places at that new club.cc.cmu.edu
address.

TTFN,
-Arthur
 
C

CBFalconer

Arthur J. O'Dwyer said:
.... snip ...

Astute readers may have noticed that I've been posting here a
lot less frequently of late, and when I do, my return address has
been at "alumni.cmu.edu" instead of "andrew.cmu.edu". Well, my
CMU student network access is about to run out for good, so if I
should drop off Usenet entirely, you'll know the reason is my lack
of a sufficiently hassle-free newsreader outside of CMU. All my
old Web stuff is in the same places at that new club.cc.cmu.edu
address.

See sig. Free newsreaders are available. Thunderbird especially.

--
Some free news servers. I use teranews and gmane.
<http://www.teranews.com> (1 time charge) (free)
<http://news.aioe.org> (free)
<http://dotsrc.org> (free)
<http://www.x-privat.org/international.php> (free)
<http://motzarella.org/?language=en> (free)
<http://gmane.org/> (mail-lists via news) (free)
<http://www.newsfeeds.com/signup.htm> (pay)
<http://www.individual.net/ (low pay)
 
J

Jack Klein

For Jack, and anyone else with links to that document:
That mirror's new address on the Web is the strikingly similar,
yet fundamentally distinct,

http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Please update your bookmarks accordingly. :)

Thank you, Arthur, I have corrected my signature.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top