473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

AL
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.
Aug 3 '08 #1
12 2204
"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.

Aug 3 '08 #2
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
Aug 3 '08 #3
"AL@TW" <schosnab...@gmail.comwrote:
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
Aug 3 '08 #4
CBFalconer <cb********@yahoo.comwrites:
"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) ks***@mib.org <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"
Aug 3 '08 #5
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.
Aug 3 '08 #6
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>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 3 '08 #7
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.
Aug 3 '08 #8

"AL@TW" <sc*********@gmail.comwrote in message
How to call a C++ function from c code?
extern "C++"?
Three ways:

1) extern "C" the C++ function in C++ code. If you don't want to touch the
C++ source, write a wrapper.

2) Compile the C code under C++ and call in the normal way. Note this means
you'll have to be careful not to use any C-only constructs, like uncast
malloc().

3) Examine the object file that the C++ compiler creates and call the
mangled function names that it outputs. This is the worst option but
cometimes necessary.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 3 '08 #9
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.
Aug 3 '08 #10
In article <91**********************************@a3g2000prm.g ooglegroups.com>,
AL@TW <sc*********@gmail.comwrote:
>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.
Check out http://www.comeaucomputing.com/techtalk/#externc
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 3 '08 #11
CBFalconer <cb********@yahoo.comwrites:
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) ks***@mib.org <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"
Aug 3 '08 #12
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>CBFalconer <cb********@yahoo.comwrites:
>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).

Aug 4 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
0
by: Mike S | last post by:
I've seen examples of using the CallWindowProc Windows API function to call functions through their addresses in VB6 -- a clever workaround to emulate C-like function pointer semantics. A...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.