Combining C and C++ code together?

asked 2014-07-15 18:42:02 -0500

Kozuch gravatar image

updated 2014-07-16 04:46:00 -0500

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?

edit retag flag offensive close merge delete

Comments

2

Could you tell us which data structures were reported as incompatible? Are you sure that the appropriate header files were included?

unxnut gravatar imageunxnut ( 2014-07-15 18:54:45 -0500 )edit
4

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.

berak gravatar imageberak ( 2014-07-15 23:59:44 -0500 )edit
2

As to combining old and new data structures, most C++ data structures can embed a C data structure. For example:

 IplImage* test; // C API data
 Mat test2 (test); // C++ API data
StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-16 02:38:17 -0500 )edit
1

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 Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-07-16 10:37:50 -0500 )edit
2

@abhishek: Please use operator IplImage to convert from Mat to IplImage. If you have IplImage i and Mat m, you can write i = m.operator IplImage();.

unxnut gravatar imageunxnut ( 2014-07-17 16:02:05 -0500 )edit