From: "Jamie Risk" <jamie_risk@yahoo.ca>
To: linux-c-programming@vger.kernel.org
Subject: Re: integrating c code and c++ code
Date: Thu, 2 Jan 2003 11:24:21 -0500 [thread overview]
Message-ID: <av1p1u$aog$1@main.gmane.org> (raw)
In-Reply-To: 15883.27533.81795.913324@cerise.nosuchdomain.co.uk
It's also useful to surround your c code with "extern c { ... code ... }"
when compiling c files with a cpp compiler. Doing so will avoid confusion
when calling C routines compiled by a cpp compiler from C routines compiled
by a C compiler (name mangling issues).
More useful still try:
#ifdef __c_plus_plus /* Check this macro defnition, it's a close variant of
this */
extern c
{
#endif
int myfunc(int a);
int myfunc(int a)
{
...
}
#ifdef __c_plus_plus
}
#endif
"Glynn Clements" <glynn.clements@virgin.net> wrote in message
news:15883.27533.81795.913324@cerise.nosuchdomain.co.uk...
>
> qhwang wrote:
>
> > Does anyone have experience of integrating c code together with c++
code? I
> > know it may be unusual but I need to do it.
>
> It isn't at all unusual to integrate C++ with C. However, calling C
> from C++ is a lot easier than calling C++ from C.
>
> > The case is, I need to integrate
> > some well defined c++ wavelet classes into my c application, but I don't
> > have enough time to rewrite the wavelet classes in plain c. So how can I
do
> > it? Any help or further pointer will be greatly appreciated. Many
thanks.
>
> You can't call C++ methods from C, only plain functions. If you want
> to call methods, you must write wrapper functions; such functions must
> use C linkage.
>
> A simple example is attached.
>
> --
> Glynn Clements <glynn.clements@virgin.net>
>
>
----------------------------------------------------------------------------
----
> /* private.h */
>
> class foo
> {
> public:
> void bar(void);
> };
>
>
----------------------------------------------------------------------------
----
> /* public.h */
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> extern void *new_foo(void);
> extern void foo_bar(void *);
>
> #ifdef __cplusplus
> }
> #endif
>
>
----------------------------------------------------------------------------
----
> /* foo.cpp */
>
> #include <iostream.h>
> #include "private.h"
> #include "public.h"
>
> void foo::bar(void)
> {
> cout << "hello, world" << endl;
> }
>
> void foo_bar(void *f)
> {
> ((foo *) f)->bar();
> }
>
> void *new_foo(void)
> {
> return (void *) new foo;
> }
>
>
----------------------------------------------------------------------------
----
> /* main.c */
>
> #include "public.h"
>
> int main(void)
> {
> void *f = new_foo();
> foo_bar(f);
> return 0;
> }
>
>
next prev parent reply other threads:[~2003-01-02 16:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-26 15:22 integrating c code and c++ code qhwang
2002-12-26 17:24 ` Joseph D. Wagner
2002-12-26 21:15 ` supersets (Was: RE: integrating c code and c++ code) Glynn Clements
2002-12-26 20:50 ` integrating c code and c++ code Glynn Clements
2003-01-02 16:24 ` Jamie Risk [this message]
2003-01-03 3:49 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='av1p1u$aog$1@main.gmane.org' \
--to=jamie_risk@yahoo.ca \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).