home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!ukma!nntp.msstate.edu!darwin.sura.net!udel!sbcs.sunysb.edu!csws12.ic.sunysb.edu!rhorn
- From: rhorn@csws12.ic.sunysb.edu (Robert Horn)
- Subject: Re: Linking C++ and C
- Message-ID: <1992Nov13.201229.6532@sbcs.sunysb.edu>
- Sender: usenet@sbcs.sunysb.edu (Usenet poster)
- Nntp-Posting-Host: csws12.ic.sunysb.edu
- Organization: State University of New York at Stony Brook
- References: <1992Nov11.141726.22547@cucs5.cs.cuhk.hk> <BxMzLn.G9G@iat.holonet.net>
- Date: Fri, 13 Nov 1992 20:12:29 GMT
- Lines: 51
-
- In article <BxMzLn.G9G@iat.holonet.net> rkinder@iat.holonet.net (Robert J. Kinder) writes:
- >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.
- >
- >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.
-
- in test1.cpp, it should help to put in the line
-
- extern "C" void f( void);
-
-
- extern "C" void f( void) {
- ....
- }
-
- this informs the c++ compiler that void f( void) should have C linkage,
- and thus, no name mangling. This is with cfront of HP-UX, so Turbo C/C++
- might behave differently.
- --
- rhorn@ic.sunysb.edu Never choose a college because it has a duckpond.
- $@` Send me hate mail, I love it.
-