How to call a C++ function from c code?

A

AL@TW

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.

Does the C++ need to be a static function?

BTW, the c code is compiled with C++ compiler.
 
C

CBFalconer

AL@TW said:
How to call a C++ function from c code?
extern "C++"?

No. In general, you can't, because of C++ overloading. You can
call C from C++, however.
 
S

Stephen Sprunk

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
 
P

Peter Nilsson

AL@TW said:
How to call a C++ function from c code?

You don't. [You can in certain cases, but the details
are implementation specific and outside the scope of
the C language itself, hence off-topic in comp.lang.c.]
extern "C++"?
I think it just inhibit the warning.
I maybe have to consider c++  object creation problem.

Does the C++ need to be a static function?

Ask about C++ in comp.lang.c++.
BTW, the c code is compiled with C++ compiler.

Then you don't have a C language question, but a C++
question. Again, ask in comp.lang.c++.
 
K

Keith Thompson

CBFalconer said:
No. In general, you can't, because of C++ overloading. You can
call C from C++, however.

<OT>
Yes, you can call C from C++, and you can call C++
from C. This is explained in the "C++ FAQ Lite",
<http://www.parashift.com/c++-faq-lite/>, section 32.

Chuck, as I recall, last time we discussed this, you refused to
believe it was possible to call C++ from C even after I demonstrated
it.

In any case, it happens to be C++, not C, that provides the
mechanisms for calling functions in either language from the other,
so this is a question a comp.lang.c++ -- but only if the C++ FAQ
doesn't answer the question.

But as Stephen Sprunk already pointed out, the OP isn't really trying
to call C++ from C anyway; since he's compiling all is code with
a C++ compiler, it's all C++ code, and there are no inter-language
calling issues at all. (What the OP is already doing may well be
the best approach anyway.)
</OT>
 
I

Ian Collins

CBFalconer said:
No. In general, you can't, because of C++ overloading.

Not that old nonsense again. I though we ponded the truth into you last
time around.
 
K

Keith Thompson

CBFalconer said:
And I cavilled above.

The problem is that the phrase "In general" is ambiguous here. I
think what you meant is that it's not always possible, which is true.
An equally reasonable reading is that it's *usually* not possible,
which is not true. (Even in the presence of overloading, you can
provide a uniquely named C++ wrapper.)

In any case, saying that "In general, you can't" is hardly responsive
to a question about how to do it.

Yes, you certainly can call C++ from C. No, you can't always do it;
there are cases where C++ features are incompatible with C, making a
C-to-C++ call impossible or impractical. Since it happens to be C++,
not C, that provides the mechanisms for cross-language calls in both
directions, comp.lang.c++ is the place to ask about it, but only if
the detailed information in the "C++ FAQ Lite" doesn't already answer
the question.

Yes, this is off-topic, but (a) I'm providing a redirection to a forum
where it's topical, and (b) I'm also correcting a statement that's
both off-topic and misleading.
 
K

Kenny McCormack

The problem is that the phrase "In general" is ambiguous here. I

But you of all people should know that Chuck's interpretation *is* the
clc-approved one. People routinly say things like "That doesn't work in
general" when it works perfectly well on 99.9999% of all known machines.

And, mathematically speaking, they are right (to do so).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top