c++ calling c functions

T

teju

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
 
I

Ian Collins

teju 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?
If you can't declare then as C, you can't access them from C.
 
K

Kira Yamato

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.
 
J

James Kanze

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.
 
J

Jim Langston

teju 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?

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.
 
S

Sjouke Burry

Kira said:
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

Ian Collins

Sjouke said:
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.
 
K

Kira Yamato

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,236
Latest member
ShondaSchu

Latest Threads

Top