Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Using C++ code in a C program/file

I have a very large program in C. I have come across source to send an email, but it is in C++. Is there a way to have embedded C++ code in my C program? Would I have to convert the email source to C instead?
1 Solution
i dont think so .. u cannot have c++ code embedded in ur c code.. ( in the sense that you will use C compiler to compile it)..
but the other way round is possible..
embedding C code in C++ code.. and obviously u'll be using C++  compiler for that..

btw if u embed C code in C++ it will look like as if u have embedded c++ code in C , u cant tell the difference.. :)

the way to do this varies slightly , from compiler to compiler.. if on linux .. u'll be using g++ and on windows most probably u'll be using VC++ (cl.exe)
 OR TC..
so go ahead, use a C++ compiler and in ur code .. write most of the part in C .. and some 'embedded' c++
What kind of C++ code is it?  There are several types:

1) The C programmer type: basically it is C but they are just putting declarations all over the place, using // as comment and using new/delete instead of malloc/free.  These can be easily converted to C: just move declarations to the top and change all new/delete to malloc/free.

2) Declared objects and methods: no inheritence/templates.  These are quite easy to convert too.  Just have a routine for every method and pass a structure round.

3) Full blown C++.  These, as akshayxx says, would be better if you compiled everything using the C++ compiler.  Just put

#ifdef __cplusplus
extern "C" {

at the head of the program and

#ifdef __cplusplus
at the end of the program on all C modules.

4) Full blown C++.  Another way is to write a wrapper around all the C++ methods.
The other thing that you can often do is create a function that uses the send e-mail class in C++ and precede it with extern "C", compile this function and the e-mail class with the C++ compiler, and then call this function from your C code; like:

// In sendemail.cxx
extern "C" int SendEmail(char *subject, char *body, char **addresses)
  Email e;
  e.subject = subject;
  // send the e-mail

// Then, in your main C code
int SendEmail(char *subject, char *body, char **addresses);

int myfunc()
  SendEmail(subject, body, addresses);

The extern "C" in my sendemail.cxx example prevents name-mangling and allows the C functions to call it.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

In the long long time ago ...
C++ compiler always convert *.CPP to *.C, before compile it to machine code.
You can't believe it now, but yes it did. It convert the object into struct, the method into modified function, etc.

So ... you may try to find an old C++ compiler.
The old compilers i.e. CFront will do old C++ i.e. V1 with no templates.  It really depends on what the code does and whether the C++ is based on C89 or not.  There are a lot of differences in the way code in interpeted between the CFront version and Version 3.
SecretSantanaAuthor Commented:
Doing exactly as mnashadka wrote worked perfectly. Using extern "C" before the class or elsewhere it did not like though. Everyone helped push me in the right direction thanks a ton.

By the way I'm using 2 platforms, MSVC 6 on my computer and gcc on linux for the server. The whole project is purely C but mixing in the single .cpp seems to work fine.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now