473,413 Members | 1,802 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,413 software developers and data experts.

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 25223
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.