OpenCV无法在自定义目录中编译

时间:2016-11-10 11:55:29

标签: c++ opencv makefile

我在Ubuntu上,想在自定义目录上安装不同版本的OpenCV(2.4.13)。我在这里遵循了这个教程:http://code.litomisky.com/2014/03/09/how-to-have-multiple-versions-of-the-same-library-side-by-side/

我无法编译这个简单的main.cpp程序。我无法创建一个cv :: Mat图像,但我可以很好地获得OpenCV版本!:

#include <iostream>
#include <opencv2/core/version.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(int argc, char ** argv)
{
std::cout << "OpenCV version: "
        << CV_MAJOR_VERSION << "." 
        << CV_MINOR_VERSION << "."
        << CV_SUBMINOR_VERSION
        << std::endl;

cv::Mat image; //without this line, it works!


return 0;
}

这是我的makefile:

CPP = g++ -std=c++0x

# OpenCV 2.4.13
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib  \
           -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include


all: test

test: main.cpp
     $(CPP) $(CPPFLAGS) $^ -o $@

这是编译器错误:

/tmp/ccyrdd7H.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccyrdd7H.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

2 个答案:

答案 0 :(得分:1)

您的Makefile错了。 -L&amp; -l选项在链接时是相关的(但在编译时)。见this&amp; that答案和示例(两者都与您的问题非常相似,并显示您应该能够适应的Makefile)。运行make -p以了解内置规则和它们使用的变量。

(我猜你是在Linux上)

详细了解ELFlinkers&amp; dynamic linkersobject filesexecutables等。请参阅Levine的Linkers and loaders书。

顺便说一句,CV_MAJOR_VERSION等......可能是某些标题中定义的macro(某些#include directives提到)。但cv::Mat可能是一些真正的C ++类,带有构造函数或链接时引用的vtable

也许您还应该阅读一本关于C ++的好书,例如: Programming -- Principles and Practice Using C++;但我们没有时间和空间来解释这里的一切。

答案 1 :(得分:0)

makefile应如下:

CPP = g++ -std=c++0x

# OpenCV trunk
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib -lopencv_core -lopencv_highgui -lopencv_imgproc  \
           -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include

all: test

test: main.cpp
    $(CPP) $(CPPFLAGS) $^ -o $@

缺少标志-lopencv_core -lopencv_highgui -lopencv_imgproc。

通过在不同目录中安装opencv,您可能会遇到无法访问的共享库错误。在这种情况下,请按照此处的说明进行操作:openCV program compile error "libopencv_core.so.2.4: cannot open shared object file: No such file or directory" in ubuntu 12.04