| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"teju" |
| Date: |
17 Dec 2007 10:22:02 PM |
| Object: |
c++ calling c functions |
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
please reply,
thanx
.
|
|
| User: "Kira Yamato" |
|
| Title: Re: c++ calling c functions |
17 Dec 2007 11:26:16 PM |
|
|
On 2007-12-17 23:22:02 -0500, teju <rao.tejaswini@gmail.com> said:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
Not in any portable way. It depends on which compiler you used and how
you built the C++ library.
--
-kira
.
|
|
|
| User: "Sjouke Burry" |
|
| Title: Re: c++ calling c functions |
18 Dec 2007 12:31:09 PM |
|
|
Kira Yamato wrote:
On 2007-12-17 23:22:02 -0500, teju <rao.tejaswini@gmail.com> said:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
Not in any portable way. It depends on which compiler you used and how
you built the C++ library.
Correct me if i am wrong, but can the C++ caller of c not pass a struct
or pointer to data to be shared with the c part? And call a C routine
to regularly process that data?
That way you leave the control to the c++ side, but gives you access to
any stucture/data you need.
.
|
|
|
| User: "Kira Yamato" |
|
| Title: Re: c++ calling c functions |
18 Dec 2007 12:54:30 PM |
|
|
On 2007-12-18 13:31:09 -0500, Sjouke Burry
<burrynulnulfour@ppllaanneett.nnlll> said:
Kira Yamato wrote:
On 2007-12-17 23:22:02 -0500, teju <rao.tejaswini@gmail.com> said:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
Not in any portable way. It depends on which compiler you used and how
you built the C++ library.
Correct me if i am wrong, but can the C++ caller of c not pass a struct
or pointer to data to be shared with the c part? And call a C routine
to regularly process that data?
That way you leave the control to the c++ side, but gives you access to
any stucture/data you need.
I'm not sure what you mean here. C++ has no trouble calling C. That
just need an extern "C" declaration in the C++ code.
The OP, on the other hand, wants to do the reverse: C calling C++
objects' methods. One way to do that is to let the C program know what
name and signature the C++ compiler has given to the particular class
method. And then perhaps use some platform specific API's, such as
dlsym(), to get to that method as a C function.
Perhaps you can try speaking C++, a.k.a. write some code to illustrate
what you're trying to say.
--
-kira
.
|
|
|
|
| User: "Ian Collins" |
|
| Title: Re: c++ calling c functions |
18 Dec 2007 12:53:42 PM |
|
|
Sjouke Burry wrote:
Kira Yamato wrote:
On 2007-12-17 23:22:02 -0500, teju <rao.tejaswini@gmail.com> said:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
Not in any portable way. It depends on which compiler you used and
how you built the C++ library.
Correct me if i am wrong, but can the C++ caller of c not pass a struct
or pointer to data to be shared with the c part? And call a C routine
to regularly process that data?
Only if the struct were a POD struct, which is why I said what I said.
--
Ian Collins.
.
|
|
|
|
|
|
| User: "James Kanze" |
|
| Title: Re: c++ calling c functions |
18 Dec 2007 03:04:51 AM |
|
|
On Dec 18, 5:22 am, teju <rao.tejasw...@gmail.com> wrote:
i have a code written in c now merged with c++ code i mean i
am calling c functions in c++ using extern "C" but now i want
to use some objects declared and defined in c++ to be
available in c code.
how can i do this?
To be made available in what way? There's certainly no problem
in passing the address of a C++ object through C code, e.g. as a
void*. On the other hand, there's no way C code will be able to
call virtual functions, using the C++ syntax.
If you want to use some new C++ object in the C code, you're
going to have to modify the C code. (For that matter, if you
were going to use some new C object in the code, you'd have to
modify it.) If you modify it, why not just switch to C++? If
the C is well written, it should compile with few changes as
C++, and you can gradually migrate it to more idiomatic C++,
using new C++ objects as desired. If parts of the code still
have to be called from C, you can declare them extern "C", and
still use all of the C++ facilities in the function itself. All
extern "C" says is that the C++ compiler should use the C
calling conventions.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
| User: "Ian Collins" |
|
| Title: Re: c++ calling c functions |
17 Dec 2007 11:21:06 PM |
|
|
teju wrote:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
If you can't declare then as C, you can't access them from C.
--
Ian Collins.
.
|
|
|
|
| User: "Jim Langston" |
|
| Title: Re: c++ calling c functions |
18 Dec 2007 11:50:40 AM |
|
|
teju wrote:
hi,
i have a code written in c now merged with c++ code i mean i am
calling c functions in c++ using extern "C" but now i want to use some
objects declared and defined in c++ to be available in c code.
how can i do this?
please reply,
thanx
Declare the C++ functions you want to call in C with extern "C" in the C++
code.
This will work in most cases, there are issues when trying to pass classes,
though, since C does not have classes, only structures.
--
Jim Langston
tazmaster@rocketmail.com
.
|
|
|
|

|
Related Articles |
|
|