Combining C and C++ code together? [closed]
I am quite a beginner to both C and C++ and I have a fundamental question - I come across various code snippets, some in C and some in C++. Sometimes, it would really be nice to be able to combine these two together. I saw OpenCV C code being saved as .cpp and compiled with g++ (example here).
I tried to combine some code - I guess C++ compiler is most likely able to compile C code, but I faced incompatible data structures between OpenCV C and C++ API. Is it possible to write hybrid code then?
Could you tell us which data structures were reported as incompatible? Are you sure that the appropriate header files were included?
if you're a beginner - stick with opencv's c++ api.
you will see a lot of code out there, that is using the old c-api, but the best advice is: if it has IplImages in it - move on.
As to combining old and new data structures, most C++ data structures can embed a C data structure. For example:
And to add to Steven's comment, To convert IplImage to Mat: If test1 is an IplImage,
Mat test2(test1);
and to convert the Mat image back to IplImage go for this:Mat test2;
IplImage test3 = test2;
IplImage* test4 = &test3;
The test4 now is your new IplImage back. :)
@abhishek: Please use
operator IplImage
to convert fromMat
toIplImage
. If you haveIplImage i
andMat m
, you can writei = m.operator IplImage();
.