Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Multiple functions with the same name


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Oct 23, 2014 2:41 PM Last Post By: Mathew Joy
Mathew Joy

Posts: 15
Registered: 8/26/11
Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2014 2:05 PM
Hi,

In my project I have multiple functions with the same name(not good, but that's a different topic). However I see that there is no compilation/link error and when I invoke the call, the call goes to a different unit while there is the same function declared and defined in the same unit. The content is different, but the signature, same. But what I notice is when I debug, I don't see the blue dot in the function that is in the same unit, so it is not linked. However if I make the other function static, the function in the same unit has the blue dot and execution goes there.

My expectation is the compiler to give me an error while linking (ambiguous) but it looks like one is preferred over the other. Has anyone got this before or is there a way the compiler chooses one over the other?

Regards,
Mathew Joy

OS: Win 7
IDE: XE4 with update1
Boba ½°

Posts: 73
Registered: 12/19/03
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2014 4:01 PM   in response to: Mathew Joy in response to: Mathew Joy
"Mathew Joy" wrote in message news:698538 at forums dot embarcadero dot com...
...

not having any experience with XEs for sure I can say that
all Borland compilers - XE compilers are not from Borland -
have this problem of not being able to distinguish the C++
standard defined calls with different signatures (*).

... compiler to give me an error while linking ...

Mat, those are two different animals: compiler and linker...
and they do not talk to each other under XE ;(

... or is there a way ...

try it with MS and see it for yourself (I'm not an advocate
for either). XE is just an IDE; you've been fighting with
VCL for at least 1/2 a year and you will master yourself in
it to find out your time was well spent :) My solution for
VCL projects I have to maintain is to avoid name collisions
when possible.
(*) the core of this problem lays deep in VCL(**) not being
able to resolve type name re-definitions coming from MS
Winblows SDK that was agreed to be compatible with early
Borland C++ compilers.
(**) in VCL, does L stand for Language?
david hoke

Posts: 616
Registered: 2/9/07
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 6:33 AM   in response to: Mathew Joy in response to: Mathew Joy
<Mathew Joy> wrote in message news:698538 at forums dot embarcadero dot com...
Hi,

In my project I have multiple functions with the same name(not good, but
that's a different topic). However I see that there is no compilation/link
error and when I invoke the call, the call goes to a different unit while
there is the same function declared and defined in the same unit. The
content is different, but the signature, same. But what I notice is when I
debug, I don't see the blue dot in the function that is in the same unit,
so it is not linked. However if I make the other function static, the
function in the same unit has the blue dot and execution goes there.

My expectation is the compiler to give me an error while linking
(ambiguous) but it looks like one is preferred over the other. Has anyone
got this before or is there a way the compiler chooses one over the other?

<mostlyignorethis...>
In earlier version linkers (2006 at least, maybe 2007), no complaint was
made about duplicate functions. I never bothered to figure out which one
was used (first encountered, last encountered, random ?), but it shouldn't
be too hard to do - just create n functions and simple program each with
same name/signature calling the function(s) once, and printf('<some
different value for each function>'); then see which gets invoked when you
call (I think the functions would have to be in separate source code
modules). Link it passing the various modules in different orders and try
it after each link.

I think (haven't noticed recently) that by the XE3 linker (I skipped
in-between, actually am using XE3 linker with rest of 2007 toolset) a
warning of some sort is generated that duplicate symbols were
encountered - but I don't think its an error.

If you're not seeing the warning, you might try (if you haven't already)
requesting detailed output, in 2007 was tools-options Environment Options,
verbosity drop down, detailed selection, and see if that at least provides
the warning.

I suppose there is the possibility that by XE4 the warning could have been
removed/disabled, if you can't find a way to produce it - or the possibility
that I'm not remembering correctly...

...trying something...

</mostlyignorethis>

Hmm - well I played with it - apparently even later it did not complain
about duplicate functions - but it does complain about duplicate (global)
variables...

In case your interested, my source:
<main.cpp>
extern "C" int func(int a) ;

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int a, rval ;
if(argc<2)
printf("usage: %s <value>\n",argv[0]),exit(1);
a = atoi(argv[1]) ;
printf("calling func") ;
rval=func(a);
printf("returned %d\n",rval);

printf("done.");

return rval ;
}

<funca.cpp>
#include <stdio.h>

int ourval ;

extern "C" int func(int a)
{
printf("one\n%d\n",a);
ourval = a ;
return 1 ;
}

<funcb.cpp>
#include <stdio.h>

int ourval ;

extern "C" int func(int a)
{
printf("two\n%d\n",a);
ourval = a ;
return 2 ;
}

<funcc.cpp>
#include <stdio.h>

int ourval ;

extern "C" int func(int a)
{
printf("three\n%d\n",a);
ourval = a ;
return 3 ;
}

david hoke

Posts: 616
Registered: 2/9/07
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 8:05 AM   in response to: david hoke in response to: david hoke
"david hoke" <dhoke.nojunk@east-shore.com> wrote in message
news:698648 at forums dot embarcadero dot com...

Hmm - well I played with it - apparently even later it did not complain
about duplicate functions - but it does complain about duplicate
(global) variables...

In case your interested, my source:
<main.cpp>

from command line could be compiled/executed:

bcc32 main.cpp funca.cpp funcb.cpp funcc.cpp -l-r
main.exe 25
bcc32 main.cpp funcb.cpp funca.cpp funcc.cpp -l-r
main.exe 26
bcc32 main.cpp funcc.cpp funcb.cpp funca.cpp -l-r
main.exe 27
Mathew Joy

Posts: 15
Registered: 8/26/11
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 8:21 AM   in response to: Mathew Joy in response to: Mathew Joy
Mathew Joy wrote:
...
Thanks for your replies, I am able to replicate. Have a VCL test app. Have another (apart from the form's cpp) unit1 that has a function say, A() have another function say, B(). A() invokes B(). Only A() is declared in unit1.h. Have another unit, unit2. unit2.cpp had B() (signature same, content different). unit2.h has B() declared. A() is invoked from test app's OnCreate, but when A() calls B() the B() of unit2 is executed. No compile or link error/warning (yes they both are technically different, but the word compiler is often interchanged with IDE or linker). I can see that unit1::B() is not linked in, as I cannot force the debug execution there, nor can I see the blue dot.

Regards,
Mathew Joy
Valence Crearer

Posts: 64
Registered: 11/27/99
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 23, 2014 5:06 AM   in response to: Mathew Joy in response to: Mathew Joy
In the old days, the order of the linker path determined which one was picked, along with the order that the module was placed in the code. The first found was the first picked.

If this is problem, namespaces would fix.

Duplicate functions with different types are commonly, thought perhaps not in this case, bugs. Follow this:
Module A is compiled with a non-inline funciton a() in a header. a() is modified for module B, and is in the same header. Now Module A and B have different versions of a(), with A, the first, being an error. If the app is then linked, or if Module A is in a library, not rebuilt, then A::a() gets linked, and the behavior changed for B is ignored. Not the desired result, and if Module A is not in the thoughts of the developer, not having an error, or at least a warning, will result in undefined behavior, completely obscure and difficult to find.

The new behavior is probably better? And, again, if really needed (doubtful), namespaces cure the problem?

Mathew Joy wrote:
Mathew Joy wrote:
...
Thanks for your replies, I am able to replicate. Have a VCL test app. Have another (apart from the form's cpp) unit1 that has a function say, A() have another function say, B(). A() invokes B(). Only A() is declared in unit1.h. Have another unit, unit2. unit2.cpp had B() (signature same, content different). unit2.h has B() declared. A() is invoked from test app's OnCreate, but when A() calls B() the B() of unit2 is executed. No compile or link error/warning (yes they both are technically different, but the word compiler is often interchanged with IDE or linker). I can see that unit1::B() is not linked in, as I cannot force the debug execution there, nor can I see the blue dot.

Regards,
Mathew Joy
Mathew Joy

Posts: 15
Registered: 8/26/11
Re: Multiple functions with the same name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 23, 2014 2:41 PM   in response to: Valence Crearer in response to: Valence Crearer
Valence Crearer wrote:
In the old days, ...
There are no two opinion that duplicate function are not good and once found there are several solutions. My contention is that the compiler should have warned me of its existence as other compilers do. Otherwise it will take some time and effort before the issue is uncovered.

Regards,
Mathew Joy
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02