OpenCV C ++ / C compilation error: undefined reference to `cv :: fastFree (void *) '

I am trying to get a C program to call a function in a C ++ file using OpenCV. I can get the C file to call a basic integer function in the C ++ file and return the result, but whenever I try to add some OpenCV code to the C ++ file, I get compilation errors. Here is my simple code in each respective module:

foo.cpp

#include <time.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv/cv.hpp"

#include <iostream>

#ifdef _cplusplus 
extern "C" int f(int);
#endif 

using namespace std;
using namespace cv;

int f(int i)
{
cout << "CPP SON: " << i << endl;
i--;

//Mat image;

//image = imread( "image1.jpg", 1 );

//namedWindow( "image1.jpg", CV_WINDOW_AUTOSIZE );

//imshow( "image1.jpg", image );

//waitKey(0);

return i;
}

      

bar.c

#include <stdio.h>

int global = 0;

int f(int);

void cc(int i)
{
    global = f(i);
    /* ... */
    printf("hello from C! %d \n", global);
}

int main(int argc, char *argv[]) {

    printf("this si is the C code called main\n");
    cc(32);


}

      

Makefile

mot : foo.o bar.o
    g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o

foo.o : foo.cpp
    g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o

bar.o : bar.c
    g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o

clean : 
    rm foo.o
    rm bar.o
    rm mot

      

Commenting out all the OpenCV lines in foo.cpp returns the following result:

bi@rtes4:~/Desktop$ make
g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o
g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o
g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o
bi@rtes4:~/Desktop$ ./mot
this si is the C code called main
CPP SON: 32
hello from C! 31 

      

After commenting out the line "Mat image;" throws the following error:

bi@rtes4:~/Desktop$ make
g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o
g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o
g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o
foo.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
foo.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
make: *** [mot] Error 1

      

Does anyone have any help they can provide? Thank you in advance for your help in this matter, we are very grateful.

+3


source to share


2 answers


This is not a compilation error, but a communication error.

I suspect you are not familiar with the C ++ build steps. Your makefile has rules for compilation (foo.o) and for linking (mot). Both now contain pkg-config --libs opencv

. However, when linking, you must pass the library; the compilation step only needs the OpenCV headers.



What is the result pkg-config --libs opencv

? This is a shell command to get your local OpenCV installation. It probably isn't configured correctly or doesn't work at all.

+1


source


The problem was the binding phase. I had to go through the shared library to the gcc compiler so that the C program could reference C ++ code that was not malformed.



0


source







All Articles