home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!apple!kaleida.com!moon!gordie
- From: gordie@kaleida.com (Gordie Freedman)
- Newsgroups: comp.lang.c++
- Subject: Re: How to call C++ functions in a C progr
- Date: 18 Dec 1992 23:25:57 GMT
- Organization: Happy hacker bait and tackle shop
- Lines: 54
- Distribution: world
- Message-ID: <1gtmm5INN5o@golden.kaleida.com>
- References: <1gqe3lINNs1v@eagle.natinst.com>
- Reply-To: gordie@kaleida.com
- NNTP-Posting-Host: moon.kaleida.com
-
- The best way to do this is have a wrapper function in the C++ code that
- the C function can call. Of course, this is not optimum performance, so
- hopefully things are encapsulated such that the calls from C to C++ are
- infrequent, and frequent calls are within the same language.
-
- By declaring foo1 and foo2 extern "C", the names do not get mangled. Note that if
- they are called from other C++ code in another file, that file must declare the
- references to foo1 and foo2 as extern "C" also.
-
- Make sure you link with the c++ linker, it is also recommended that you compile
- main with a c++ compiler, to make sure that global constructors get called/etc.
-
- -----------foo.C-----------------
- // This is C++ code
- extern "C" int foo1 (int, int);
- extern "C" int foo2 (int, int);
-
- int bar (int x, int y)
- {
- int someInt;
- // do some fancy C++ stuff
- return someInt;
- }
-
- int foo1 (int x, int y)
- {
- return bar (x, y);
- }
-
- int foo2 (int x, int y)
- {
- int z;
- // Do some fancy C++ stuff
- return z;
- }
- ---------test.c-----------------
-
- /* This is C code */
- extern int foo1 (int, int);
- extern int foo2 (int, int);
-
- void
- test (void)
- {
- /* stuff ... */
- a = foo1 (1, 2);
- /* More stuff */
- a = foo2 (10, 20);
- }
-
- ---
- Gordie Freedman: gordie@kaleida.com Sigs are for kids
-
-
-