By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

multiple function definitions

P: n/a
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

Mar 19 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
su**************@yahoo.com, India said:

<snip>
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 19 '07 #2

P: n/a

<su**************@yahoo.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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).
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Mar 19 '07 #3

P: n/a
On Mon, 19 Mar 2007 15:54:27 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:
su**************@yahoo.com, India said:

<snip>
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.
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>.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 20 '07 #4

P: n/a
On 19 Mar 2007 07:21:32 -0700, "su**************@yahoo.com, India"
<su**************@yahoo.comwrote in comp.lang.c:
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
<string.h>.
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.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 20 '07 #5

P: n/a
Jack Klein said:
On Mon, 19 Mar 2007 15:54:27 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:
>su**************@yahoo.com, India said:

<snip>
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.

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 20 '07 #6

P: n/a

On Mon, 19 Mar 2007, Jack Klein wrote in his .sig:
>
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
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
Mar 21 '07 #7

P: n/a
"Arthur J. O'Dwyer" wrote:
>
.... 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)

--
Posted via a free Usenet account from http://www.teranews.com

Mar 21 '07 #8

P: n/a
On Wed, 21 Mar 2007 01:55:43 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*******@alumni.cmu.eduwrote in comp.lang.c:
>
On Mon, 19 Mar 2007, Jack Klein wrote in his .sig:

alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

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
Mar 22 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.