how to call C++ from C?

D

Digital Puer

I have some C++ code that I would like to call from within a C program
(main() is within the C code). How do I go about doing this portably?
I know that I can find the mangled name of the C++ functions, but
this is kind of weird.

I am using gcc and g++ on Linux, if that matters.


Here is an example of what I'm doing:


---------------------------------------------------
a.c:
---------------------------------------------------
#include "b.h"


int main()
{
foo();
}


---------------------------------------------------
b.h
---------------------------------------------------
int foo();



---------------------------------------------------
b.cpp
---------------------------------------------------
#include <map>
int foo()
{
map<int, int> m;

}


---------------------------------------------------
Makefile
---------------------------------------------------
a.out: a.o b.o
g++ a.o b.o

a.o: a.c
gcc -c a.c

b.o: b.cpp
g++ -c b.cpp
 
K

Kevin Goodsell

Digital said:
I have some C++ code that I would like to call from within a C program

C does not define any way to do this, making this off-topic in
comp.lang.c. C++ sort of defines a way to do it, so comp.lang.c++ is
appropriate. Cross-posted and followups set.

[Remainder of message left intact for clc++.]
 
M

Malcolm

Digital Puer said:
I have some C++ code that I would like to call from within a C
program (main() is within the C code). How do I go about doing this
portably?
I know that I can find the mangled name of the C++ functions, but
this is kind of weird.
Convert your C code to C++ code. Generally this is quite easy, all you need
to do is change the file extension and a few implicit casts of void *s (this
is an illustration why the advice not to cast the return from malloc() isn't
necessarily very good).
There are a few niggly incompabilities between C and C++ which could just
wreck things, such as the fact that sizeof('c') yields sizeof(char) in C++
and sizeof(int) in C. Also you have to be careful if you are using stdin /
stdout and cin / cout in the same program.
 
M

Martijn Lievaart

and sizeof(int) in C. Also you have to be careful if you are using stdin /
stdout and cin / cout in the same program.

No you don't, they're tied together. I should be no problem.

HTH,
M4
 
M

Martijn Lievaart

I have some C++ code that I would like to call from within a C program
(main() is within the C code). How do I go about doing this portably?
I know that I can find the mangled name of the C++ functions, but
this is kind of weird.

I am using gcc and g++ on Linux, if that matters.

It shouldn't.

Basically, you want your main to be C++. This has to do with the necessary
startup code, C++ startup should include C startup, but not vice versa.
Many implementations don't seem to have this restriction, gcc seems to be
among them. Yet it is a simple way to increase portability.

Next, declare any C++ function that should be callable from main as extern
"C". See http://www.cae.tntech.edu/help/programming/mixed_languages/view
for more information.

HTH,
M4
 
K

Kevin Goodsell

Malcolm said:
Convert your C code to C++ code. Generally this is quite easy, all you need
to do is change the file extension and a few implicit casts of void *s (this

There's no such thing as an "implicit cast". Casts are explicit by
definition.
is an illustration why the advice not to cast the return from malloc() isn't
necessarily very good).

The advice not to cast malloc is fine (for most purposes). Your advice
to convert a source file from one language to another, on the other
hand, is questionable. It amounts to wasted effort creating bad C++ code
from (possibly) good C code, and quite possibly introducing bugs, when
the OP should obviously just use the technique C++ provides for
interfacing the two languages.
There are a few niggly incompabilities between C and C++ which could just
wreck things, such as the fact that sizeof('c') yields sizeof(char) in C++
and sizeof(int) in C. Also you have to be careful if you are using stdin /
stdout and cin / cout in the same program.

As already pointed out, this is not true.

-Kevin
 
C

Christopher Benson-Manica

In comp.lang.c Martijn Lievaart said:
No you don't, they're tied together. I should be no problem.

To be quite precise, here is what the illustrious Mr. Stroupstrup says:

"C and C++ I/O can be mixed on a per-character basis. A call of
sync_with_stdio() before the first stream I/O operation in the
execution of a program guarantees that the C-style and C++-style I/O
operations share buffers. A call of sync_with_stdio(false) before the
first stream I/O operatio prevents buffer sharing and can improve I/O
performance on some implementations."

(Stroustrup, B. The C++ Programming Language, Special Edition, p651)
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top