Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!ccu.umanitoba.ca!herald.usask.ca!alberta!brazeau.ucs.ualberta.ca!unixg.ubc.ca!ubc-cs!uw-beaver!zephyr.ens.tek.com!uunet!sdl.mdcbbs.com!alanb From: alanb@sdl.mdcbbs.com Newsgroups: comp.lang.c++ Subject: Re: call C++ from C Message-ID: <1991Jun28.143521.1@sdl.mdcbbs.com> Date: 28 Jun 91 13:35:21 GMT References: <359@soleil.jupiter.UUCP> <20463@alice.att.com> Organization: Shape Data Ltd. (McDonnell Douglas M&E, Cambridge UK) Lines: 40 Nntp-Posting-Host: shapeg Nntp-Posting-User: alanb In article <20463@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes: > In article <359@soleil.jupiter.UUCP> rh@jupiter.UUCP (#Rachid Himmi) writes: > >> I'm looking for the way to call a C++ module from a C program through >> the declaration of a class and the call of its function members. > > Turn your C program into a C++ program and it's easy. > Otherwise, it's a matter between you and your implementer(s) > and it's not guaranteed to work no matter what you do. > -- > --Andrew Koenig > ark@europa.att.com In version 1, functions not declared as overloaded generally got the same name as C functions would. In version 2, the linkage specification extern "C" can be used to ensure this (Annotated C++ Reference Manual, Ellis and Stroustrup, section 7.4) e.g. class class1 { public: void foo(); }; // C++ definition extern "C" void CLASS1_foo( struct class1_t *me ) { me->foo(); } /* C header file */ extern void CLASS1_foo( struct class1_t *me ); Season with friend declarations if you need a C interface to private functions, and cast/constructor operators to turn your classes into C types. (If you use structs in C++, they will be the same in C, but normally I would expect the module to be passing out an opaque type. "(void *)this" will often do, but having a type that reflects the C++ type is cleaner.) alanb@sdl.mdcbbs.com Alan Braggins #include