home *** CD-ROM | disk | FTP | other *** search
- 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
- From: rkinder@iat.holonet.net (Robert J. Kinder)
- Newsgroups: comp.lang.c++
- Subject: Re: Linking C++ and C
- Message-ID: <BxMzLn.G9G@iat.holonet.net>
- Date: 13 Nov 92 04:10:34 GMT
- References: <1992Nov11.141726.22547@cucs5.cs.cuhk.hk>
- Organization: HoloNet (BBS: 510-704-1058)
- Lines: 46
-
- hclam@cuse1.se.cuhk.hk (LAM Ho Cheong) writes:
- : I got a problem of linking C & C++ programs using the Turbo C++ for
- : Windows on PC. I'm going to link some C source codes and some C++
- : source codes. I need to invoke certain C-like functions defined in a C++
- : codes in a C codes. However, the linker replies me that no such functions
- : have been declared. That means the functions defined in the C++ codes are
- : not accessable from the C codes. Can anyone give me some advice?
- :
- : test1.cpp test2.c
- : ... main()
- : void f(void) {
- : { ...
- : printf("Hello, World\n"); f();
- : } ...
- : ... }
- :
- : linker error: "function _f is not declared in module "test2.c""!
- :
- : It's natural because I'm using some more advanced stuff (C++) by a less
- : advanced stuff (C). But, I need to solve this ...
- :
- : I'll appreciate any advice sent to me at hclam@se.cuhk.hk
- :
- : Thanks in advance.
-
- C++ is backward compatible with C. There is all kinds of 'behind the
- scenes' stuff that goes on when initializing a C++ program. I'm not all
- surprised that the linker couldn't find the C++ function f() identifier,
- probably due to C++ 'name mangling' if not several other things, too.
-
- I suggest going to the Options | Compiler | C++ Options menu and set the
- C++ compile switch ON regardless of the file extention (.C or .CPP), even
- though your test2.c is a plain C module.
-
- Also, add the declaration 'extern void f( void );' somewhere in the first
- few lines of your test2.c module to tell the compiler the function resides
- somewhere else. This is normally done in a 'header file' (.h or .hpp) and
- #include'd in client modules.
-
- Always use function prototypes!
-
- --
- // rkinder@holonet.net International Software Solutions, Inc. -
- // Robert J. Kinder, Jr. Boca Raton, Florida -
- // 1-800-788-4774 -
- // "No thanks! I have a radio."
-