我试图编译和链接,我发现下面的OpenCV程序在线:在Ubuntu 14.04上从源代码编译时如何链接到opencv 3.0?
main.cpp中:
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
VideoCapture capture(0);
while(capture.isOpened())
{
Mat frame;
if (! capture.read(frame))
continue;
imshow("lalala", frame);
if(waitKey(10) == 27)
break;
}
return 0;
}
我filestructure如下:
〜的/ dev/opencv- 1
〜的/ dev/OpenCV的-示例
OpenCV的-1是一个git克隆:https://github.com/Itseez/opencv
我跟着位于中的说明:http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html
我的目标是要调试到OpenCV的代码,所以我用了CMAKE_BUILD_TYPE =调试
我能得到OpenCV的代码编译sucessfuly。我写了下面的makefile编译这个样本:
INCPATH =-I../opencv-1/modules/objdetect/include \
-I../opencv-1/modules/highgui/include \
-I../opencv-1/modules/imgproc/include \
-I../opencv-1/modules/core/include \
-I../opencv-1/modules/imgcodecs/include \
-I../opencv-1/modules/videoio/include \
LIBPATH =-L../opencv-1/debug/lib
LIBS =-lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopencv_video -lopencv_objdetect
%:%.cpp
g++ $(INCPATH) $(LIBS) $(LIBPATH) $^ -o [email protected]
编译我跑:使主
发生这种情况时,我得到以下链接错误:
g++ -I../opencv-1/modules/objdetect/include -I../opencv-1/modules/highgui/include -I../opencv-1/module s/imgproc/include -I../opencv-1/modules/core/include -I../opencv-1/modules/imgcodecs/include -I../open cv-1/modules/videoio/include -lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopenc v_video -lopencv_objdetect -L../opencv-1/debug/lib main.cpp -o main
/tmp/cc0PMDdW.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `cv::VideoCapture::VideoCapture(int)'
main.cpp:(.text+0x69): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
main.cpp:(.text+0xce): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
main.cpp:(.text+0xf6): undefined reference to `cv::waitKey(int)'
main.cpp:(.text+0x131): undefined reference to `cv::VideoCapture::isOpened() const'
main.cpp:(.text+0x14d): undefined reference to `cv::VideoCapture::~VideoCapture()'
main.cpp:(.text+0x1b0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/cc0PMDdW.o: In function `cv::String::String(char const*)':
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4f): undefined reference to `cv::String::al locate(unsigned long)'
/tmp/cc0PMDdW.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallo cate()'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray()':
main.cpp:(.text._ZN2cv11_InputArrayC2Ev[_ZN2cv11_InputArrayC5Ev]+0x13): undefined reference to `vtable for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray(cv::Mat const&)':
main.cpp:(.text._ZN2cv11_InputArrayC2ERKNS_3MatE[_ZN2cv11_InputArrayC5ERKNS_3MatE]+0x17): undefined re ference to `vtable for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::~_InputArray()':
main.cpp:(.text._ZN2cv11_InputArrayD2Ev[_ZN2cv11_InputArrayD5Ev]+0x13): undefined reference to `vtable for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::_OutputArray(cv::Mat&)':
main.cpp:(.text._ZN2cv12_OutputArrayC2ERNS_3MatE[_ZN2cv12_OutputArrayC5ERNS_3MatE]+0x23): undefined re ference to `vtable for cv::_OutputArray'
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::~_OutputArray()':
main.cpp:(.text._ZN2cv12_OutputArrayD2Ev[_ZN2cv12_OutputArrayD5Ev]+0x13): undefined reference to `vtab le for cv::_OutputArray'
/tmp/cc0PMDdW.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc0PMDdW.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::dea llocate()'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
我不是确定我错过了什么。我已经包含了这个简单程序需要编译的所有库。我在网上搜索了类似的错误,并且我看到的所有答案都建议添加已经存在于我的makefile中的库。
还将'g ++ $(INCPATH)$(LIBS)$(LIBPATH)$^-o $ @'重写为$(CXX)$(INCPATH)$(LIBPATH)$^$(LIBS)-o $ @ '。 – Chnossos 2014-12-07 21:57:02
@berak,我很困惑。我以为我用我的LIB_PATH变量来做这件事。 – lordoku 2014-12-08 01:05:33
@Chnossos非常感谢。这似乎已经成功了。我要回答你的答案并为这个问题创建一个答案。 – lordoku 2014-12-08 17:06:13