Newbie quesiton: Mix C and C++

P

philbo30

I have an app that is written in C. Unfortunately there are things
that C cannot do so I need to "call" some C++ functions in order to,
for example, generate charts and graphs as .png files. Since these
are two different languages, is this even realistic? Where's the best
reference on mixing C, C++ ? In advance, thank you for any info
provided.
 
G

gw7rib

I have an app that is written in C. Unfortunately there are things
that C cannot do so I need to "call" some C++ functions in order to,
for example, generate charts and graphs as .png files. Since these
are two different languages, is this even realistic? Where's the best
reference on mixing C, C++ ? In advance, thank you for any info
provided.

Mixing the two languages is possible. Alternatively, writing the whole
thing in C++ may be a better option - most C code does not take too
much re-writing to make it valid C++.

If you read the C++ FAQ at http://www.parashift.com/c++-faq-lite/
there is a whole chapter about mixing the two languages.

If you do hit problems, you may be better off asking questions in
comp.lang.c++ .

Hope this helps.
Paul.
 
P

philbo30

Mixing the two languages is possible. Alternatively, writing the whole
thing in C++ may be a better option - most C code does not take too
much re-writing to make it valid C++.

If you read the C++ FAQ athttp://www.parashift.com/c++-faq-lite/
there is a whole chapter about mixing the two languages.

If you do hit problems, you may be better off asking questions in
comp.lang.c++ .

Hope this helps.
Paul.

Thanx for the info; I was hoping it was as simple as something like:

extern
call cppfunction()
return to C

Unfortunately it doesn't seem that simple :-(
 
G

gw7rib

Thanx for the info; I was hoping it was as simple as something like:

extern
call cppfunction()
return to C

Unfortunately it doesn't seem that simple :-(

It can be fairly simple - decide which C++ functions are going to be
called from C functions, and put extern "C" in front of their
prototypes. If I remember correctly.

Paul.
 
B

Ben Pfaff

philbo30 said:
I have an app that is written in C. Unfortunately there are things
that C cannot do so I need to "call" some C++ functions in order to,
for example, generate charts and graphs as .png files.

C code is perfectly able to generate charts and graphs as .png
files. I don't know where you get the idea that it cannot.
 
R

rafaelc

C code is perfectly able to generate charts and graphs as .png
files. I don't know where you get the idea that it cannot.

He probably already have library for doing that and it's coded in C++.
 
K

Keith Thompson

philbo30 said:
I have an app that is written in C. Unfortunately there are things
that C cannot do so I need to "call" some C++ functions in order to,
for example, generate charts and graphs as .png files. Since these
are two different languages, is this even realistic? Where's the best
reference on mixing C, C++ ? In advance, thank you for any info
provided.

C++ provides facilities for calling C from C++ and/or vice versa; C
does not.

The comp.lang.c++ FAQ probably has the information you need. If not,
try the newsgroup itself.

Hint: extern "C".
 
P

Pierre Asselin

philbo30 said:
I have an app that is written in C. Unfortunately there are things
that C cannot do so I need to "call" some C++ functions in order to,
for example, generate charts and graphs as .png files. Since these
are two different languages, is this even realistic? Where's the best
reference on mixing C, C++ ? In advance, thank you for any info
provided.

Off the top of my head,
1) Create an interface to the C++ library, written in C++
but declared with extern "C" bindings, so it can be
called from C. This will require some creative work
if the C++ is heavily object-oriented.
2) You may have to write main() in C++; if so, change
the original C main() to something like C_main() and
call it from the C++ main().
3) You may have to link with the C++ compiler.
4) If your C compiler supports C++ exceptions, enable it.
Otherwise, you have to catch all exceptions in your C++
interface layer.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top