?
Solved

Using C++ code in a C program/file

Posted on 2003-03-14
6
Medium Priority
?
185 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:SecretSantana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8141851
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++
0
 
LVL 11

Expert Comment

by:cup
ID: 8142002
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" {
#endif

at the head of the program and

#ifdef __cplusplus
}
#endif
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.
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 80 total points
ID: 8142237
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:Kocil
ID: 8142437
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.
0
 
LVL 11

Expert Comment

by:cup
ID: 8147923
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.
0
 

Author Comment

by:SecretSantana
ID: 8149044
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question