简体   繁体   English

OpenCV C ++ / C编译错误:对`cv :: fastFree(void *)'的未定义引用

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

I am trying to have a C program call a function in a C++ file that uses OpenCV. 我正在尝试让C程序在使用OpenCV的C ++文件中调用函数。 I can get the C file to call a basic integer function in a C++ file and return a result, but whenever I try to add some OpenCV code to the C++ file, I get compilation errors. 我可以让C文件调用C ++文件中的基本整数函数并返回结果,但每当我尝试将一些OpenCV代码添加到C ++文件时,我就会遇到编译错误。 Here is my simple code in each respective module: 这是我在每个模块中的简单代码:

foo.cpp 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 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 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

When commenting out all lines of OpenCV in the foo.cpp file, the following output is returned: 在foo.cpp文件中注释掉所有OpenCV行时,将返回以下输出:

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 

Upon commenting out the line "Mat image;" 在评论出“Mat图像”这一行时 yields 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 anybody have any help they can lend? 有人可以借给他们任何帮助吗? Thank you kindly in advance for your assistance with this matter, it is greatly appreciated. 非常感谢您提前帮助解决此事,非常感谢。

It's not a compilation error, but a linking error. 这不是编译错误,而是链接错误。

I suspect that you're not familiar with the build phases of C++. 我怀疑你不熟悉C ++的构建阶段。 Your makefile has rules for compiling (foo.o) and for linking (mot). 你的makefile有编译(foo.o)和链接(mot)的规则。 Both now contain pkg-config --libs opencv . 两者现在都包含pkg-config --libs opencv Yet you should only pass the library when linking; 但你应该只在链接时通过图书馆; the compilation step needs just the OpenCV headers. 编译步骤只需要OpenCV头。

What's the output of pkg-config --libs opencv ? 什么是pkg-config --libs opencv的输出pkg-config --libs opencv That's a shell command to obtain your local OpenCV installation. 这是获取本地OpenCV安装的shell命令。 It's probably configured wrong or not at all. 它可能配置错误或根本没配置。

The problem was with the linking phase. 问题在于连接阶段。 I had to pass along the shared library to the gcc compiler so that the C program would be able to reference the C++ code that has not been mangled. 我必须将共享库传递给gcc编译器,以便C程序能够引用尚未被破坏的C ++代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM