Re: How do linkers deal with C++ duplicate code?

David L Moore <dlmoore@molalla.net>
30 Aug 1998 10:18:02 -0400

          From comp.compilers

Related articles
[8 earlier articles]
Re: How do linkers deal with C++ duplicate code? jacob@jacob.remcomp.fr (1998-08-22)
Re: How do linkers deal with C++ duplicate code? ian@cygnus.com (1998-08-22)
Re: How do linkers deal with C++ duplicate code? mrs@kithrup.com (1998-08-22)
Re: How do linkers deal with C++ duplicate code? mrs@kithrup.com (1998-08-22)
Re: How do linkers deal with C++ duplicate code? bowdidge@watson.ibm.com (Robert Bowdidge) (1998-08-24)
Re: How do linkers deal with C++ duplicate code? joachim.durchholz@munich.netsurf.de (Joachim Durchholz) (1998-08-25)
Re: How do linkers deal with C++ duplicate code? dlmoore@molalla.net (David L Moore) (1998-08-30)
Re: How do linkers deal with C++ duplicate code? zalman@netcom.com (1998-08-31)
| List of all articles for this month |

From: David L Moore <dlmoore@molalla.net>
Newsgroups: comp.compilers
Date: 30 Aug 1998 10:18:02 -0400
Organization: One World Internetworking, Inc.
References: 98-08-147 98-08-161 98-08-181
Keywords: C++, linker

Joachim Durchholz wrote:


> ian@cygnus.com wrote:


> > That is,
> > int foo (int i) {}
> > and
> > double foo (int i) {}
> > will often use the same name for foo. g++ uses foo__Fi for both.


To which Joachim Durchholz replied:


> Which is as thing should be. In C++, funcctions must not differ by
> return type only.


And John remarked:


> [I'd still think it would be a good idea to make the linker able to
> diagnose this error. It's not hard, the DTSS linker did argument type
> checking 20 years ago. -John]


Unfortunately, you cannot do both without a module concept. There are
really two types of error you want to detect:


type 1:


File 1:
float foo(int i); // no definition in this file


File 2:
double foo(int i) {} // here is the definition - oops


and


File 1:
float foo(int i) {} // definition


File 2:
double foo(int i) {} // second definition




Original C++ style mangling (no return type) detects error 2, and not
error 1.


Variant mangling (mangle the return type too) detects error 1 but not
error 2.


Which is better? Bug 1 is hard to find and common in badly written
code. One should never declare anything an the body of a module but
should import the header for the module containing the definition but
you know people don't always do this. (Perhaps someone had the other
header file checked out - I meant to fix it later)


So, bug one'll get you.


What about bug 2? Sure, your program is non-conforming, but each
module will call the function it thought it was calling. If you mix
them in one module, you get a compilation error. If you go run
Montana(say) you get an error anyway, and you fix it. If you don't get
an error, your program works. No harm no foul.


I think it is quite clear you should also mangle the return type.
[It's easy enough to arrange to catch both errors in the linker. Just
add yet another special case hack for C++. It'd be about the 50th. -John]




--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.