* integrating c code and c++ code
@ 2002-12-26 15:22 qhwang
2002-12-26 17:24 ` Joseph D. Wagner
2002-12-26 20:50 ` integrating c code and c++ code Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: qhwang @ 2002-12-26 15:22 UTC (permalink / raw)
To: linux-c-programming
Hi folks,
Does anyone have experience of integrating c code together with c++ code? I
know it may be unusual but I need to do it. 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.
QingHua Wang
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: integrating c code and c++ code
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
1 sibling, 1 reply; 6+ messages in thread
From: Joseph D. Wagner @ 2002-12-26 17:24 UTC (permalink / raw)
To: 'qhwang', linux-c-programming
Just combine the two. C++ is a subset of C which means everything that
worked under C will continue to work under C++.
Sure, it will result in sloppy code, a weird mix of C and C++ syntax, but it
will compile and work fine.
Or at least it should 8-)
Joseph Wagner
-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of qhwang
Sent: Thursday, December 26, 2002 9:23 AM
To: linux-c-programming@vger.kernel.org
Subject: integrating c code and c++ code
Hi folks,
Does anyone have experience of integrating c code together with c++ code? I
know it may be unusual but I need to do it. 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.
QingHua Wang
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: supersets (Was: RE: integrating c code and c++ code)
2002-12-26 17:24 ` Joseph D. Wagner
@ 2002-12-26 21:15 ` Glynn Clements
0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2002-12-26 21:15 UTC (permalink / raw)
To: linux-c-programming
Joseph D. Wagner wrote:
> Just combine the two. C++ is a subset of C
C++ definitely isn't a subset of C. It's almost a superset of C (which
is presumably what you meant), but not quite. E.g.:
struct foo {
int dummy;
};
typedef int foo;
is perfectly legal in C but not in C++.
Also, it's quite possible to write a program which is valid in both C
and C++, but does something different in each[1].
For a language L2 to truly be a superset of language L1, every program
which is valid in L1 must be valid in L2 and have exactly the same
semantics in both.
[1] Actually, the same is technically true of C variants which support
C++ "//" comments compared to ANSI C; e.g.:
a = b//*comment*/c
;
is equivalent to:
a = b/c;
in ANSI C, but recognising C++ comments changes the interpretation to:
a = b;
Admittedly this is something of a "corner case", which a programmer
would probably avoid. But not all C code is generated by humans. E.g.
compilers for high-level languages often use C as a portable assembly
language (i.e. they generate C then spawn a C compiler rather than
directly generating machine code for the target platform).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: integrating c code and c++ code
2002-12-26 15:22 integrating c code and c++ code qhwang
2002-12-26 17:24 ` Joseph D. Wagner
@ 2002-12-26 20:50 ` Glynn Clements
2003-01-02 16:24 ` Jamie Risk
1 sibling, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2002-12-26 20:50 UTC (permalink / raw)
To: qhwang; +Cc: linux-c-programming
[-- Attachment #1: message body and .signature --]
[-- Type: text/plain, Size: 760 bytes --]
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>
[-- Attachment #2: private.h --]
[-- Type: text/plain, Size: 58 bytes --]
/* private.h */
class foo
{
public:
void bar(void);
};
[-- Attachment #3: public.h --]
[-- Type: text/plain, Size: 143 bytes --]
/* public.h */
#ifdef __cplusplus
extern "C" {
#endif
extern void *new_foo(void);
extern void foo_bar(void *);
#ifdef __cplusplus
}
#endif
[-- Attachment #4: foo.cpp --]
[-- Type: text/plain, Size: 236 bytes --]
/* 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;
}
[-- Attachment #5: main.c --]
[-- Type: text/plain, Size: 101 bytes --]
/* main.c */
#include "public.h"
int main(void)
{
void *f = new_foo();
foo_bar(f);
return 0;
}
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: integrating c code and c++ code
2002-12-26 20:50 ` integrating c code and c++ code Glynn Clements
@ 2003-01-02 16:24 ` Jamie Risk
2003-01-03 3:49 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: Jamie Risk @ 2003-01-02 16:24 UTC (permalink / raw)
To: linux-c-programming
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;
> }
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: integrating c code and c++ code
2003-01-02 16:24 ` Jamie Risk
@ 2003-01-03 3:49 ` Glynn Clements
0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2003-01-03 3:49 UTC (permalink / raw)
To: Jamie Risk; +Cc: linux-c-programming
Jamie Risk wrote:
> 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).
Using 'extern "C" ...' only forces C linkage; the code is still
compiled according to the rules of C++, which isn't necessarily
compatible with C. The only robust solution is not to compile C code
with a C++ compiler (or ensure that your code is 100% valid as both C
and C++, which is harder than it seems; just getting 100% valid ANSI C
is difficult enough).
> #ifdef __c_plus_plus /* Check this macro defnition, it's a close variant of
> this */
It's actually "__cplusplus".
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-01-03 3:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2003-01-03 3:49 ` Glynn Clements
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).