473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

multiple function definitions

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
8 2172
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

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

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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Magnusson | last post by:
I have a problem with multiple definitions that I can't quite straighten out. I have a templated class defined inside a namespace, and I want to create a function in that namespace that works on...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
4
by: JackyMove | last post by:
Dear all, I have encount the following problem. I have compiled a library "lib.lib" successfully using a simulator compatable to VC++ on Windows platform. Then I try to build an executatble in...
4
by: Andrew | last post by:
Hello, I am recieving a multiple definition error from the linker when I try to build a project I am working on. The message states that the functions I defined within an external .c source file...
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
3
by: jparulan | last post by:
Hi All, I'm using SOAP3.0. I was able to successfully call a WSDL file and get a value properly. But when the WSDL changed to have a MULTIPLE <element name> it was failing. This code works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.