home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16257 < prev    next >
Encoding:
Text File  |  1992-11-14  |  2.7 KB  |  64 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ukma!nntp.msstate.edu!darwin.sura.net!udel!sbcs.sunysb.edu!csws12.ic.sunysb.edu!rhorn
  3. From: rhorn@csws12.ic.sunysb.edu (Robert Horn)
  4. Subject: Re: Linking C++ and C
  5. Message-ID: <1992Nov13.201229.6532@sbcs.sunysb.edu>
  6. Sender: usenet@sbcs.sunysb.edu (Usenet poster)
  7. Nntp-Posting-Host: csws12.ic.sunysb.edu
  8. Organization: State University of New York at Stony Brook
  9. References: <1992Nov11.141726.22547@cucs5.cs.cuhk.hk> <BxMzLn.G9G@iat.holonet.net>
  10. Date: Fri, 13 Nov 1992 20:12:29 GMT
  11. Lines: 51
  12.  
  13. In article <BxMzLn.G9G@iat.holonet.net> rkinder@iat.holonet.net (Robert J. Kinder) writes:
  14. >hclam@cuse1.se.cuhk.hk (LAM Ho Cheong) writes:
  15. >: I got a problem of linking C & C++ programs using the Turbo C++ for
  16. >: Windows on PC. I'm going to link some C source codes and some C++ 
  17. >: source codes. I need to invoke certain C-like functions defined in a C++
  18. >: codes in a C codes. However, the linker replies me that no such functions
  19. >: have been declared. That means the functions defined in the C++ codes are
  20. >: not accessable from the C codes. Can anyone give me some advice?
  21. >: 
  22. >: test1.cpp                          test2.c
  23. >: ...                                main()
  24. >: void f(void)                       {
  25. >: {                                     ...
  26. >:     printf("Hello, World\n");         f();
  27. >: }                                     ...
  28. >: ...                                }
  29. >: 
  30. >: linker error: "function _f is not declared in module "test2.c""!
  31. >: 
  32. >: It's natural because I'm using some more advanced stuff (C++) by a less 
  33. >: advanced stuff (C). But, I need to solve this ...
  34. >: 
  35. >: I'll appreciate any advice sent to me at hclam@se.cuhk.hk
  36. >: 
  37. >: Thanks in advance.
  38. >
  39. >C++ is backward compatible with C.  There is all kinds of 'behind the
  40. >scenes' stuff that goes on when initializing a C++ program.  I'm not all
  41. >surprised that the linker couldn't find the C++ function f() identifier,
  42. >probably due to C++ 'name mangling' if not several other things, too.
  43. >
  44. >Also, add the declaration 'extern void f( void );' somewhere in the first
  45. >few lines of your test2.c module to tell the compiler the function resides
  46. >somewhere else.  This is normally done in a 'header file' (.h or .hpp) and
  47. >#Include'd in client modules.
  48.  
  49. in test1.cpp, it should help to put in the line
  50.  
  51.   extern "C" void f( void);
  52.  
  53.  
  54.   extern "C" void f( void) {
  55.    ....
  56.   }
  57.  
  58. this informs the c++ compiler that void f( void) should have C linkage,
  59. and thus, no name mangling. This is with cfront of HP-UX, so Turbo C/C++
  60. might behave differently.
  61. -- 
  62. rhorn@ic.sunysb.edu         Never choose a college because it has a duckpond.
  63. $@`                                             Send me hate mail, I love it.
  64.