Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!csus.edu!ucdavis!csusac!unify!Unify.com!grp From: grp@Unify.com (Greg Pasquariello) Newsgroups: comp.lang.c++ Subject: Re: Help! calling C++ functions from a C main program Message-ID: <1991Apr17.095517@Unify.com> Date: 17 Apr 91 16:55:17 GMT References: <1991Apr15.163426.14250@murdoch.acc.Virginia.EDU> Sender: news@Unify.Com (news admin) Reply-To: grp@Unify.com (Greg Pasquariello) Organization: Unify Corporation, Sacramento, CA, USA Lines: 45 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