home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!yale.edu!jvnc.net!newsserver.technet.sg!nuscc!argon!suresh
- From: suresh@argon.iss.nus.sg (Suresh Thennarangam - Research Scholar)
- Newsgroups: comp.lang.c++
- Subject: Re: How to call C++ functions in a C program?
- Message-ID: <1992Dec22.103610.29839@nuscc.nus.sg>
- Date: 22 Dec 92 10:36:10 GMT
- References: <1gqbepINNt6s@function.mps.ohio-state.edu> <1gqe3lINNs1v@eagle.natinst.com>
- Sender: usenet@nuscc.nus.sg
- Reply-To: suresh@iss.nus.sg (Suresh Thennarangam - Research Scholar)
- Organization: Institute of Systems Science, NUS, Singapore
- Lines: 49
-
- In article <1gqe3lINNs1v@eagle.natinst.com> banshee@natinst.com (Jeff Kellam) writes:
- >In article <1gqbepINNt6s@function.mps.ohio-state.edu> ren@function.mps.ohio-state.edu (Liming Ren) writes:
- >>I know we can call C functions inside a C++ program. Is there a way to
- >>call a C++ function inside a C program? I tried to link a C program with
- >>a C++ function as follows:
- >>
- >>Where f.o has a very simple C++ function inside and g.c is a simple C
- >>program calling the function inside f.o. It dose not WORK!
- >
- >Basically, you can't.
- >
- >The C++ compiler mangles the name of the C++ function, thus the linker won't
- >match it with the name you'd expect from C.
- >
- >
- >Of course, even if you get the name mangling taken care of, you'd still
- >have to handle things like explicitly passing a pointer to the object and
- >setting up virtual tables similar ugly things. Thus my original answer,
- >you can't.
-
- You can really.
-
- You need to write a C wrapper for the C++ function and
- prevent the C++ compiler from mangling the wrapper name by encasing
- it in an "extern C" declaration. Then you can call the wrapper and
- the linker will resolver the references correctly. For quick hacks
- you can dispense with the wrapper scheme and just encase the C++
- function name in the aforementioned "extern C" declaration. However
- wrappers are an elegant solution to other problems that may arise.
- An earlier post of mine said a bit more about this subject in answer
- to a very similar question. For a good detailed treatment please look
- at "The Sun C++ programmers Guide" Appendix G.
-
- Regards and Merry Christmas,
-
-
- __
- (_ / / o_ o o |_
- __)/(_( __) (_(_ /_)| )_
-
-
- ***************************************************************************
- * Suresh Thennarangam * EMail: suresh@iss.nus.sg(Internet) *
- * Research Scholar * ISSST@NUSVM.BITNET *
- * Institute Of Systems Science * Tel: (065) 772 2588. *
- * National University Of Singapore * Facs.: (065) 778 2571 *
- * Heng Mui Keng Terrace * Telex: ISSNUS RS 39988 *
- * Singapore 0511. * *
- ***************************************************************************
-