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

  1. Path: sparky!uunet!know!mips2!news.bbn.com!usc!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!agate!iat.holonet.net!rkinder
  2. From: rkinder@iat.holonet.net (Robert J. Kinder)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Linking C++ and C
  5. Message-ID: <BxMzLn.G9G@iat.holonet.net>
  6. Date: 13 Nov 92 04:10:34 GMT
  7. References: <1992Nov11.141726.22547@cucs5.cs.cuhk.hk>
  8. Organization: HoloNet (BBS: 510-704-1058)
  9. Lines: 46
  10.  
  11. hclam@cuse1.se.cuhk.hk (LAM Ho Cheong) writes:
  12. : I got a problem of linking C & C++ programs using the Turbo C++ for
  13. : Windows on PC. I'm going to link some C source codes and some C++ 
  14. : source codes. I need to invoke certain C-like functions defined in a C++
  15. : codes in a C codes. However, the linker replies me that no such functions
  16. : have been declared. That means the functions defined in the C++ codes are
  17. : not accessable from the C codes. Can anyone give me some advice?
  18. : test1.cpp                          test2.c
  19. : ...                                main()
  20. : void f(void)                       {
  21. : {                                     ...
  22. :     printf("Hello, World\n");         f();
  23. : }                                     ...
  24. : ...                                }
  25. : linker error: "function _f is not declared in module "test2.c""!
  26. : It's natural because I'm using some more advanced stuff (C++) by a less 
  27. : advanced stuff (C). But, I need to solve this ...
  28. : I'll appreciate any advice sent to me at hclam@se.cuhk.hk
  29. : Thanks in advance.
  30.  
  31. C++ is backward compatible with C.  There is all kinds of 'behind the
  32. scenes' stuff that goes on when initializing a C++ program.  I'm not all
  33. surprised that the linker couldn't find the C++ function f() identifier,
  34. probably due to C++ 'name mangling' if not several other things, too.
  35.  
  36. I suggest going to the Options | Compiler | C++ Options menu and set the
  37. C++ compile switch ON regardless of the file extention (.C or .CPP), even
  38. though your test2.c is a plain C module.
  39.  
  40. Also, add the declaration 'extern void f( void );' somewhere in the first
  41. few lines of your test2.c module to tell the compiler the function resides
  42. somewhere else.  This is normally done in a 'header file' (.h or .hpp) and
  43. #include'd in client modules.
  44.  
  45. Always use function prototypes!
  46.  
  47. -- 
  48. // rkinder@holonet.net            International Software Solutions, Inc.  -
  49. // Robert J. Kinder, Jr.                             Boca Raton, Florida  -
  50. //                                                        1-800-788-4774  -
  51. //                    "No thanks!  I have a radio."
  52.