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,286
147
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,286
147
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
18
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