Project with c and C ++ files

Is it possible to create a project containing some parts written in c and other parts written in C ++? Is it possible?

+6
c ++ c
source share
6 answers

Yes.

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

#ifdef __cplusplus extern "C" { #endif // normal header stuff here #ifdef __cplusplus }; #endif 

Thus, they can be correctly interpreted when included with both C code files and CPP.

If you include C code in your C ++ through the header, and it does not include the code above, and you do not have enough control over it to make the necessary changes, be sure to use, for example.

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

Please note that you can also use this as a modifier for ads, for example:

 extern "C" void someFunction(); 

Please note that C ++ has this mechanism for importing C functionality. C does not have one for importing C ++, and an attempt to include C ++ code in the C compilation unit will quickly end up with a bunch of error messages. One consequence of this is that your main function should be C ++.

+8
source share

You need a compiler that can compile both languages ​​(I have not heard of a C ++ compiler that cannot do this), or compile them with the corresponding compiler and link them (in this case @ sje397 answers). There is a good explanation on this in the C ++ FAQ Lite .

+2
source share
+2
source share

Yes, it is very possible. In fact, usually legacy systems reorganized later usually have legacy code, which is C as a kernel, but with C ++ wrappers on top of it.

+1
source share

Yes, you can. C ++ is, first of all, a superset of C. Some exceptions are possible, but in most cases it is quite normal to include projects written in C in C ++

+1
source share

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

+1
source share

All Articles