6

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

4
  • Why make it complex. Pick a language and stick to it! Commented Sep 23, 2010 at 12:09
  • @Martin York: I disagree. (1) we should have less rules, not more (2) it gets the computer closer to understanding us (via development of techniques and tools to cope). Granted, not all projects can afford to contribute to this pie-in-the-sky goal ;) Commented Sep 23, 2010 at 12:35
  • 1
    Less moving parts less chance of breaking. Commented Sep 23, 2010 at 12:42
  • Yes, that's what I meant when I said not all projects can afford to contribute toward the effort of ironing out the kinks: not enough time, motivation, money, whatever. It will obviously introduce some challenges to use more than one language. But it will also provide some valuable lessons and experience. Commented Sep 23, 2010 at 13:02

6 Answers 6

9

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++.

Sign up to request clarification or add additional context in comments.

10 Comments

You do not need that when you compile the C code with a C++ compiler.
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.
@Space_C0wb0y: compiling C with a C++ compiler is a bad idea, since valid C often isn't valid C++.
@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.
@Martin York: fixed the guard. Ty.
|
2

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.

1 Comment

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).
2

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

Comments

1

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.

Comments

1

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.

2 Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.