home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18300 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.8 KB

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