Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to call a C++ function from c code?

Reply
Thread Tools

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

 
 
AL@TW
Guest
Posts: n/a
 
      08-03-2008
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.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-03-2008
"AL@TW" wrote:
>
> 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      08-03-2008
AL@TW wrote:
> 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.)
</OT>

> 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      08-03-2008
"AL@TW" <schosnab...@gmail.com> wrote:
> 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++.

--
Peter
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-03-2008
CBFalconer <> writes:
> "AL@TW" wrote:
>> 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.


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

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-03-2008
CBFalconer wrote:
> "AL@TW" wrote:
>> How to call a C++ function from c code?
>> extern "C++"?

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

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-03-2008
Richard Heathfield wrote:
> Ian Collins said:
>
>> CBFalconer wrote:
>>> "AL@TW" wrote:
>>>> How to call a C++ function from c code?
>>>> extern "C++"?
>>> 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.

>
> <croak>
> Ribbit. Ribbit.
> </croak>
>



--
Ian Collins.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-03-2008
Ian Collins wrote:
> CBFalconer wrote:
>> "AL@TW" wrote:
>>
>>> How to call a C++ function from c code?
>>> extern "C++"?

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


And I cavilled above.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-03-2008
CBFalconer <> writes:
> Ian Collins wrote:
>> CBFalconer wrote:
>>> "AL@TW" wrote:
>>>
>>>> How to call a C++ function from c code?
>>>> extern "C++"?
>>>
>>> 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.

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

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      08-04-2008
In article <>,
Keith Thompson <kst-> wrote:
>CBFalconer <> writes:
>> Ian Collins wrote:
>>> CBFalconer wrote:
>>>> "AL@TW" wrote:
>>>>
>>>>> How to call a C++ function from c code?
>>>>> extern "C++"?
>>>>
>>>> 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.

>>
>> And I cavilled above.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57