Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Mixing C and C++ Message-ID: <670@taumet.com> Date: 15 Apr 91 16:12:54 GMT References: <72092@brunix.UUCP> Organization: Taumetric Corporation, San Diego Lines: 33 sdm@cs.brown.edu (Scott Meyers) writes: >Suppose I'm writing a program in both C and C++, and I've got globals >requiring compile-time initialization in both. Then both languages will >want to own "main" (i.e., have main written in their language). Is there a >nice way to solve this problem... >I assume that both languages generate a routine "real_main"... >However, only one real_main() will get called, the one belonging to >whichever language writes main. Is there a portable solution? What you describe is common in implementations of C++, but does not usually apply to C. C++ allows specification of runtime code to be executed (conceptually) before main(), and (conceptually) after main() exits. C provides no such facility to the C programmer, and it is unusual for a C compiler to require that main() be compiled by that C compiler (but some have that requirement for other reasons). If you use a C++-to-C translator, it is usually packaged to work with a particular C compiler (or any of a group), and you just write main() in C++. The C compiler sees main(), and does whatever it needs to do with it. Of the C++ object-code compilers I am familiar with, all provide a companion C compiler, and none have any requirement on main() that would prevent mixing C and C++ code. Some C++ object-code compilers do not even require main() to be written in C++. So: is your question theoretical, or do you actually have separate C and C++ compilers which each require main() to be compiled by that compiler? -- Steve Clamage, TauMetric Corp, steve@taumet.com