Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!rex!uflorida!gatech!mcnc!borg!oscar!tell From: tell@oscar.cs.unc.edu (Stephen Tell) Newsgroups: comp.lang.c++ Subject: Re: bug with making C++ functions have C linkage ? Message-ID: <3407@borg.cs.unc.edu> Date: 24 Apr 91 01:44:05 GMT References: <1991Apr16.122905.23613@cs.nott.ac.uk> <675@taumet.com> <1991Apr22.081254@Unify.com> Sender: news@cs.unc.edu Organization: University of North Carolina, Chapel Hill Lines: 63 In article <1991Apr22.081254@Unify.com> grp@Unify.com (Greg Pasquariello) writes: >In article <675@taumet.com>, steve@taumet.com (Stephen Clamage) writes: >> >> 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. >[Iostreams with 2.0 provide an initializer object...] >[but not all class libraries do] >> 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. Another C++ hacker and I figured out a way to do this for a simulator that was written in C and allows user code to be linked in with the program. Patch the .o files from the system so that the C main is renamed to somthing like "MaIn." Write short C++ file consisting of: extern "C" MaIn(int argc; char **argv); main(int argc, char **argv) { return MaIn(argc, argv); } This way, the real main is written in C++ and the constructors get called. The other code that you link in with the system can then be C++ and use whatever classes it likes since static objects will have had their constructors called. This may not be ideal, but it works. We did the C++ stuff with g++. I don't know what C compiler the original program was written with. It requires that the product be shipped with ordinary .o files that you run "ld" on to make customized versions, instead of funky schemes to attach user code to a running program. >> Steve Clamage, TauMetric Corp, steve@taumet.com >Greg Pasquariello grp@unify.com >Unify Corporation Be good and never poison people Steve -- Steve Tell tell@cs.unc.edu H: +1 919 968 1792 #5L Estes Park apts CS Grad Student, UNC Chapel Hill. W: +1 919 962 1845 Carrboro NC 27510 Duke Blue Devils: 1991 NCAA Basketball National Champions! We're Number 1 !! UNLV 90-91 record: "34 and DUKE."