tags:

views:

162

answers:

6

Can I have a project that has some parts written in c and other parts written in c++ ? Is this possible ?

+1  A: 

Yes it is very much possible. In fact usually legacy systems refactored later on usually have legacy code which is C as the core but with C++ wrappers on top of it.

Als
+1  A: 

Yes you can. C++ is mainly a superset of C. There might be some exceptions, but for the most part it is quite normal to include stuff written in C in your C++ projects.

Daren Thomas
Mainly being the operative word. The weird gotcha's will kill you.
Martin York
But the weird gotcha's is what makes C++ fun... It's called OOP :p
Daren Thomas
+1  A: 

Yes, you can have a project with both C and C++ code.

Brian Campbell
+8  A: 

Yes.

If you have control of the C code, then inside your C header files you should have:

#ifdef __cplusplus
extern "C" {
#endif

// normal header stuff here

#ifdef __cplusplus
};
#endif

That way they can be properly interpreted when included by both C and CPP code files.

If you include C code in your C++ via a header, and it doesn't include the code above, and you don't have enough control of it to make the necessary modifications, be sure to use e.g.

extern "C" {
#include "some_c_header.h"
};

Note that you can use this as a modifier for declarations too, e.g.:

extern "C" void someFunction();

Note that C++ has this mechanism for importing C functionality. C doesn't have one for importing C++, and trying to include C++ code in a C compilation unit will pretty quickly end in a bunch of error messages. One consequence of this is that your main function will need to be C++.

sje397
You do not need that when you compile the C code with a C++ compiler.
Space_C0wb0y
No but it's a good idea in any case. This way you can link to code already compiled with a 'plain' C compiler, and your code is more portable.
sje397
@Space_C0wb0y: compiling C with a C++ compiler is a bad idea, since valid C often isn't valid C++.
Mike Seymour
@Mike: That is true, but, as the link in my answer explains, it can be made to work if you can change the C-code (restrict it to the part of C that is a subset of C++), and that might even be beneficial for the quality of the code.
Space_C0wb0y
@Martin York: fixed the guard. Ty.
sje397
@Space_C0wb0y: True: But if your C code has been compiled by a C compiler (which most build systems will do (unless you override the default behavior)) then you will need to do this. IF you compile the C code with a C++ compiler then it is not necessary. Note: gcc -> C compiler, g++ -> C++ compiler.
Martin York
@Martin York: More general point taken :) Edited.
sje397
It should also be noted that `main` should be in a C++ source file and the application should be linked to the C++ runtime libraries. This is needed to ensure everything gets initialised correctly.
Bart van Ingen Schenau
@Bart van Ingen Schenau: I think that's obvious. You'll get errors trying to include C++ code into your C compilation units.
sje397
@sje: Some might find it obvious, some less so. If you have a mixed-language project with languages that don't share such a common heritage as C and C++, it is less clear which startup code should be used. I am not even sure there is an answer for all reasonable combinations of languages.
Bart van Ingen Schenau
@Bart: My main point was that it just won't work if you try it the other way here. But I think you're right - a note wouldn't hurt.
sje397
+2  A: 

You need a compiler that can compile both languages (I have not heard of a C++ compiler that cannot do that), or compile them with a fitting compiler each and link them (in which case the answer of @sje397 applies). There is a good explanation on the subject in the C++ FAQ Lite.

Space_C0wb0y
Yet most build systems will use the compiler appropriate for the language. Thus they will use the C compiler to compile the C code and the C++ compiler to compile the C++ code (even if the C++ compiler can potentially compile the C code). This is the standard behavior of all build systems (Make/Visual Studio/Eclipse etc).
Martin York
+2  A: 

How to mix C and C++:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

Tommy
+1: Good source of info
ArunSaha