Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Mixing C with C++ Message-ID: <690@taumet.com> Date: 24 Apr 91 15:51:57 GMT References: <1991Apr22.064847.16929@cs.nott.ac.uk> <1991Apr23.133931.6716@ssd.kodak.com> Organization: Taumetric Corporation, San Diego Lines: 46 strollo@ssd.kodak.com (larry strollo ) writes: >In article <1991Apr22.064847.16929@cs.nott.ac.uk> gas@cs.nott.ac.uk () writes: >>> This is the easy bit. The second step involves linking using C++ and >>> not C. Most of my replies pointed this out, but I had done this and >>> still encountered problems (sorry for not pointing this out before). >I'm confused. Since the linker is just linking object files (generated from >C I might add), what is special about the C++ "linker" ? Usually linking is done via the cc or CC driver program (or whatever it is called on your system). The C++ version of the driver (or the driver when told to operate in C++ mode) does different things for C++ than for C. If you know the ultimate linking command, you can use that command directly, if less conveniently. Sometimes the C++ driver runs separate post-linking commands, which you would have to remember to do as well. >I'm working on a set of programs that are going to be implemented in C++, >but must provide an interface callable from C. It's probably not going to >be reasonable for me to insist that the user have Cfront. Am I in trouble ? Not necessarily. All C++ compilers I am familiar with require that main() be compiled with with the C++ compiler. This does not mean that the user must have a C++ compiler to use your programs. You write the actual main() in its own module like this: extern int my_main(int, char**); main(int argc, char** argv) { return my_main(argc, argv); } Compile it with the C++ compiler, and put it in your library. Instruct your users to write their main() with the name "my_main" instead. Everything should just work. In some cases, the user might want main() to take additional parameters, one common variation being main(int argc, char** argv, char** environment) You might have to provide several versions of main() in that case, and at most one of them could be put into a library; the others would have to be separate object files. -- Steve Clamage, TauMetric Corp, steve@taumet.com