473,469 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to call C++ from C?

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

"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
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
Martijn Lievaart writes:
[It] should be no problem.


Nov 14 '05 #5

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
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...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
7
by: archana | last post by:
Hi all, I am having application in which i am doing asynchronous call.I am using manualresetevent to wait for asynchronous call to complete. I want to stop asynchronous call after certain...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.