C++ code in C

Boo025

Member
Oct 6, 2006
79
0
66
Note: I'm not too good with programming... but learning

I have a code that has been written by someone else in C++ using Visual C++ that I need to integrate with C code (compiling in Cygwin). I tried bluntly by just doing #include and it gave me an error due to parsing (think it doesn't recognize the class structure).

If I'm not mistaken, the C++ class structure is not supported in C. Is there a way to get around this problem?
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
Are you compiling with gcc? If so, then compile with g++ instead. If you still have problems, post back with more info about the code and I'll do my best to help.
 

Boo025

Member
Oct 6, 2006
79
0
66
I believe there's a script for Makefile to compile anything in *.c using gcc, and *.cpp using g++.

And the file I was trying to modify is a *.c so I'm guessing it was compiling in gcc and giving me error when I added C++ code in.

I'll try find a way around this and compile, and let you know tomorrow... (this thing takes a million year to compile)
 

Boo025

Member
Oct 6, 2006
79
0
66
Hmm, I think that worked (no error so far).

Now I have the code I'm trying to integrate in *.cpp, and existing code in *.c, but I need a way to access functions from cpp to c code.

How can I do this? Because if I do the #include again, wouldn't it give me an error?
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
You'll probably want to use the c++ (g++) compiler to compile things rather then using a C compiler. Unless there is some feature in C that isn't currently present in C++ that you are using, c should compile with any c++ compiler. (not always, but most always)
 

Boo025

Member
Oct 6, 2006
79
0
66
I manage to get my part of code to use C++ compiler while the other code uses C compiler.

Now the problem I have is that how can I access C++ function within C? I don't think I can simply include my header file onto the C code since that would barf on me. I've done some reading at C++ FAQ and it seems I can do it by just adding the 'external "C"' line; however, I'm not sure how to modify the constructor to make it work.

I was thinking something like this in the header file:
// In the header file...

extern "C" { Foo * Foo(Foo *this); /* Constructor? */

void FooFunctionA(Foo *this); /* Function A for Foo? */

void FooFunctionB(Foo *this); /* Function B for Foo? */ }


Not sure how I'd call each function in C though.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
IIRC you can declare the C++ function as a static function and I believe that will carry over. However, I really don't see what you are gaining by being able to use objects in C (Why not just use C++? the entire reason it was created).

If you get really desperate, I believe you can make a function pointer to the C++ functions and access those in C instead. But really, you aren't gaining a lot by mixing these two languages.
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
I just had a thought, you ARE aware that Cygwin supports g++ , right? And that you can compile C++ code with it directly ?
 

hans007

Lifer
Feb 1, 2000
20,212
17
81
i think if you wanted to make this work, you could compile your C++ code into a shared library or DLL. I'm not sure how to do it in unix, but I'm pretty sure if you extern C your functions in the dll then whatever loads that library will see them as just C functions.

I'm a microsoft developer though, so I'm not sure what command it is to load a dll in gcc (whatever is the equivalent of loadlibrary)
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: hans007
i think if you wanted to make this work, you could compile your C++ code into a shared library or DLL. I'm not sure how to do it in unix, but I'm pretty sure if you extern C your functions in the dll then whatever loads that library will see them as just C functions.

I'm a microsoft developer though, so I'm not sure what command it is to load a dll in gcc (whatever is the equivalent of loadlibrary)

http://tldp.org/HOWTO/Program-.../shared-libraries.html
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |