delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/11/02:56:53

Date: Mon, 11 Jan 1999 09:55:37 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: "to comp.os.msdos.djgpp" <axlq AT unicorn DOT us DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Calling C++ functions from C functions: how?
In-Reply-To: <77boug$l58$1@samba.rahul.net>
Message-ID: <Pine.SUN.3.91.990111095512.3893N-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On 11 Jan 1999, to comp.os.msdos.djgpp wrote:

> I have a library that contains several C modules and two C++ modules.
> The C modules end in .c and the C++ modules end in .cc.  I'm using RHIDE
> to build everything.  No special compiler options.
> 
> I also have a test program in C that calls these functions.
> 
> When I attempt to link my program to the library, the linker complains
> that two functions called by the test program are undefined.  These two
> functions happen to be the C++ ones in the library.

The easiest solution for this is to recompile your test program as a
C++ program (e.g., rename its source file to .cc, or pass the "-x c++"
option to the compiler).  Since C is almost a proper subset of C++,
you should have no problems doing that.

The problem you are experiencing happens because the C++ compiler
mangles the names of C++ functions.  It does so to make functions that
have the same name but different argument types appear as different
symbols to the linker, otherwise the linker won't be able to link a
program that uses those functions.

But the C compiler doesn't know about these mangling rules, so it
instructs the linker to look for the original, unmangled name of the
function.  And that fails.

> I'm trying to port a rather large application that uses mixed C and C++
> functions, which compiled and ran fine under another compiler (ZTC).
> But with DJGPP, I'm running into this problem with the linker.

I'd guess that the other compiler compiled the C code as C++.
Otherwise, I'm not aware of any simple way out of this mess, with any
compiler.

There's a way to allow C++ functions to call C functions (by declaring
the C functions ``extern "C"''), but I don't think the other way
around is possible.

> It doesn't help to compile my test program as a C++
> program, and invoking the compiler with -x c++ seems to corrupt
> something in the object files.

Please describe the problems with compiling test program as C++ in
more detail.  I don't think there should be any such problems, and
since this is your only way out, you should IMHO concentrate on
solving these problems.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019