Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!emory!att!pacbell.com!pacbell!sactoh0!unify!Unify.com!grp From: grp@Unify.com (Greg Pasquariello) Newsgroups: comp.lang.c++ Subject: Re: bug with making C++ functions have C linkage ? Message-ID: <1991Apr19.133158@Unify.com> Date: 19 Apr 91 20:31:58 GMT References: <1991Apr16.122905.23613@cs.nott.ac.uk> Sender: news@Unify.Com (news admin) Reply-To: grp@Unify.com (Greg Pasquariello) Organization: Unify Corporation, Sacramento, CA, USA Lines: 68 In article <1991Apr16.122905.23613@cs.nott.ac.uk>, gas@cs.nott.ac.uk (Alan Shepherd) writes: > > From my understanding, the correct way to make C++ functions > accessible from modules compiled with c is to delcare them extern "C" > as in the following example: > > #include > > extern "C" void tryCout(); > > void tryCout() > { > cout << "Hello World\n"; > } > > Now this can be called from a program compiled with cc. For example, > the following program: > > extern void tryCout(); > > main() > { > tryCout(); > } > > This happily compiles without any warnings. However, when running, a > segv occurs in the cout operation. This segv disappears if both > modules are compiled with CC instead of just the first. Adding > another level of indirection to the code by writing another function > to call tryCout doesn't work either. > > Is this a bug ? It certainly looks like it ! You need to declare, in your C++ source code, an object of class Iostream_init. The constructor for the class will initialize cin, cout, cerr, and clog, and your core dump will go away. Note that this relies on C++ 2.0 and the iostreams package. To try it out, do: #include extern "C" void tryCout(); void tryCout() { Iostream_init io; cout << "Hello World\n"; } On the C side: extern void tryCout(); main() { tryCout(); } > Alan Shepherd -- --- Greg Pasquariello grp@unify.com Unify Corporation Be good and never poison people