AL@TW said:
How to call a C++ function from c code?
extern "C++"?
I think it just inhibit the warning.
I maybe have to consider c++ object creation problem.
This is OT for comp.lang.c; comp.lang.c++ is down the hall, to the left.
<OT>
The answer I assume you'll get is that the C++ function declaration
should be enclosed in an extern "C" { ... } block when compiled in C++,
while the prototype declaration that the C compiler sees should _not_ be
so enclosed. The usual way to do this is with the BEGIN_C_DECLS and
END_C_DECLS macros (Google for the idiomatic definitions) in a single
header that can be used by compilers of both languages.
I don't recall if the C++ compiler needs the function definition to be
enclosed with extern "C" { ... } or whether it will "remember" to
compile the function C-style simply by having the declaration enclosed.
> Does the C++ need to be a static function?
No. However, some features of C++ aren't available in a function that
is being compiled as extern "C", like default arguments, variable
signatures, etc. I'm pretty sure there's no way to do it with a class
method either, whether static or not, just plain functions. A common
tactic is to have a C-compatible "wrapper" function, which in turn calls
the real C++ functions. (For instance, an object may be passed in to
the wrapper via a void* parameter, and the wrapper then casts it to an
object and invokes a method on the result.)
> BTW, the c code is compiled with C++ compiler.
If you're compiling it with a C++ compiler, the code is C++. You may
have used a subset of C++ that looks the same as C (and hopefully has
the same meaning, which isn't guaranteed), but it isn't C unless you're
using a C compiler.
S