home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18130 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  1.8 KB

  1. Path: sparky!uunet!spool.mu.edu!sdd.hp.com!swrinde!news.dell.com!natinst.com!natinst.com!not-for-mail
  2. From: banshee@natinst.com (Jeff Kellam)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to call C++ functions in a C program?
  5. Date: 17 Dec 1992 11:41:09 -0600
  6. Organization: National Instruments, Austin, TX
  7. Lines: 43
  8. Distribution: world
  9. Message-ID: <1gqe3lINNs1v@eagle.natinst.com>
  10. References: <1gqbepINNt6s@function.mps.ohio-state.edu>
  11. NNTP-Posting-Host: eagle.natinst.com
  12.  
  13. In article <1gqbepINNt6s@function.mps.ohio-state.edu> ren@function.mps.ohio-state.edu (Liming Ren) writes:
  14. >Hi , Merry chrismas , everybody!
  15. >
  16. >I hope this queston makes sense.
  17. >
  18. >I know we can call C functions inside a C++ program. Is there a way to
  19. >call a C++ function inside a C program? I tried to link  a C program with 
  20. >a C++ function as follows:
  21. >
  22. >cc f.o g.c
  23. >
  24. >Where f.o has a very simple C++ function inside and g.c is a simple C
  25. >program calling the function inside f.o. It dose not WORK!
  26. >
  27. >How to do this ? Please help!
  28. >
  29. >Many  thanks!
  30.  
  31. Basically, you can't.
  32.  
  33. The C++ compiler mangles the name of the C++ function, thus the linker won't
  34. match it with the name you'd expect from C.  
  35.  
  36. If you know the name mangling scheme on your C++ compiler, you **might** be
  37. able to use that name.  There are two limitations on this:
  38.  
  39. 1.  C prepends an underscore to function names.  The mangling scheme your
  40.     C++ compiler might not.
  41.  
  42. 2.  Both of the C++ compilers I use include in the mangled name characters
  43.     like @ and $ which are not valid in a C function name.
  44.  
  45. Of course, even if you get the name mangling taken care of, you'd still
  46. have to handle things like explicitly passing a pointer to the object and
  47. setting up virtual tables similar ugly things.  Thus my original answer,
  48. you can't.
  49.  
  50. Regards,
  51.  
  52. jeff k
  53.      
  54. -- 
  55. test
  56.