By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

how to call C++ from C?

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Digital Puer wrote:
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++.]
(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

--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

P: n/a

"Digital Puer" <di**********@hotjail.comet> wrote in message
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.

Nov 14 '05 #3

P: n/a
On Sat, 10 Jan 2004 09:15:10 +0000, Malcolm wrote:
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

Nov 14 '05 #4

P: n/a
Martijn Lievaart writes:
[It] should be no problem.


Nov 14 '05 #5

P: n/a

osmium <r1********@comcast.net> wrote in message
news:bt************@ID-179017.news.uni-berlin.de...
Martijn Lievaart writes:
[It] should be no problem.


Famous last words! I have had problems due to this.

Sorry for the blank post.
Nov 14 '05 #6

P: n/a
On Sat, 10 Jan 2004 07:40:09 -0800, osmium wrote:

osmium <r1********@comcast.net> wrote in message
news:bt************@ID-179017.news.uni-berlin.de...
Martijn Lievaart writes:
> [It] should be no problem.


Famous last words! I have had problems due to this.

Sorry for the blank post.


Well as I messed up as well (I instead of It) who am I to object. :-)

M4

Nov 14 '05 #7

P: n/a
On Fri, 09 Jan 2004 16:28:17 -0800, Digital Puer wrote:
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/progr...languages/view
for more information.

HTH,
M4
Nov 14 '05 #8

P: n/a
Malcolm wrote:
"Digital Puer" <di**********@hotjail.comet> wrote in message
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


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #9

P: n/a
In comp.lang.c Martijn Lievaart <m@remove.this.part.rtij.nl> wrote:
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)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.