Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: bug with making C++ functions have C linkage ? Message-ID: <675@taumet.com> Date: 18 Apr 91 16:30:17 GMT References: <1991Apr16.122905.23613@cs.nott.ac.uk> Organization: Taumetric Corporation, San Diego Lines: 40 gas@cs.nott.ac.uk (Alan Shepherd) writes: >#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. Almost all current C++ compilers require that main() be compiled by the C++ compiler, even if other modules are compiled with a compatbile C compiler. Your compiler manual should tell you this. In general, C++ programs will require initialization of static objects via their constructors at runtime; cout is such an object. The C++ compiler arranges for the constructors (and destructors) to be called via special code emitted in the module in which main() appears. This ensures the special code is emitted and called exactly once. In your example, main() was not compiled by the C++ compiler, cout was not initialized, and the program failed when it tried to use member functions of cout. -- Steve Clamage, TauMetric Corp, steve@taumet.com