Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!chalmers.se!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.lang.c++ Subject: Re: bug with making C++ functions have C linkage ? Message-ID: <1357@appli.se> Date: 21 Apr 91 00:19:38 GMT References: <1991Apr16.122905.23613@cs.nott.ac.uk> Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 41 haydens@natasha.juliet.ll.mit.edu (Hayden Schultz) writes: >I'm pretty sure that Jim (jac@gandalf.llnl.gov) has the right answer, >that because the C++ main does tricky things like call global >constructors that the original C main didn't call. So it makes sense >that when you try to access a global object that hasn't been >initialized correctly, like cout, you get a segv. >But isn't there some way to explicitly invoke the global constructors >and destructors so you can use a C main and call C++ routines? I can't >manage to accomplish this with Sun CC. >It would be very nice if I could use C++ routines everywhere I use C >routines. For example, I can call C routines from lisp, but since it >doesn't have a C++ main, I can't figure out how to call C++ routines. Yes, that's tricky, but it ought to be possible with a little hacking. If your lisp implementation supports calling lisp functions from C/C++, and you have a binary editor, you should be able to patch in another name for the C++ "main", e.g "cppm" or something like that, and then call it from lisp the usual way. When this "main" gets called it should execute the global constructors for your C++ program, and then do a callback to your lisp function, which later will call other C++ functions. At the time of the return of this callback function the C++ "main" should execute the global destuctors before returning to lisp. This is of course very unportable since not every compiler puts the global constructor calling in "main", but rather before "main" is called, though I think AT&T derived C++ implementations will do it inside main. I also think that a binary editor won't be necessary in such implementations since they generate C output which could be edited with e.g. a sed script to change the name of the "main" function. Hope this helps Niklas -- Niklas Hallqvist Phone: +46-(0)31-40 75 00 Applitron Datasystem Fax: +46-(0)31-83 39 50 Molndalsvagen 95 Email: niklas@appli.se S-412 63 GOTEBORG, Sweden mcsun!sunic!chalmers!appli!niklas