[comp.lang.c++] Help! calling C++ functions from a C main program

fs8j@hopper.cs.Virginia.EDU (Fraser M. Street) (04/15/91)

Hi - I'm desperate for help.

I have a C graphics application that, due to the nature of the source code,
can only be compiled with a C compiler.  I now need to use a relatively 
elaborate C++ class library to generate some data that will be used by 
my graphics code.  All this code needs to be linked into one executable.

I understand how to call a C function from a C++ main program (using 
"extern "C" foo();"), but I don't know how to make it work in the other
direction - i.e. call my C++ foobar() function from my main.c file.  I
have tried separately compiling and linking the two .o files but the
linker can't 
resolve the C++ function names.

Is there a way for me to do this?


Our environment is a network of SUN3 and SUN4 machines, all running Unix.
The available compilers at this site are cc, CC, gcc, g++, and Saber C++.

Please E-mail me any ideas.  Thanks in advance.  The project's due Friday!!

Fraser 

grp@Unify.com (Greg Pasquariello) (04/17/91)

In article <1991Apr15.163426.14250@murdoch.acc.Virginia.EDU>,
fs8j@hopper.cs.Virginia.EDU (Fraser M. Street) writes:

> Hi - I'm desperate for help.
> 
> I have a C graphics application that, due to the nature of the source code,
> can only be compiled with a C compiler.  I now need to use a relatively 
> elaborate C++ class library to generate some data that will be used by 
> my graphics code.  All this code needs to be linked into one executable.
> 
> I understand how to call a C function from a C++ main program (using 
> "extern "C" foo();"), but I don't know how to make it work in the other
> direction - i.e. call my C++ foobar() function from my main.c file.  I
> have tried separately compiling and linking the two .o files but the
> linker can't 
> resolve the C++ function names.

Extern "C" will compile the specified functions without mangling the names.
Thus, you can use it to compile C++ functions that you want to call from
C as well as C functions that you want to call from C++.

Simply declare your C++ foobar() to use extern "C" linkage, then call 
foobar() from your C main.

Careful using iostreams from your C++ code.  They will probably dump core
because they haven't been initialized.  In order to initialize them, you
may need to declare the class Iostream_init.  It's constructor will initialize
things properly for you.

> 
> Is there a way for me to do this?
> 
> 
> Our environment is a network of SUN3 and SUN4 machines, all running Unix.
> The available compilers at this site are cc, CC, gcc, g++, and Saber C++.
> 
> Please E-mail me any ideas.  Thanks in advance.  The project's due Friday!!
> 
> Fraser 

--

---
Greg Pasquariello	grp@unify.com
Unify Corporation 	Be good and never poison people