终端的示例程序无法运行

时间:2013-11-11 05:59:31

标签: opencv ubuntu

我是ubuntu中的opencv新手。我按照here中的教程来设置linux中的opencv。但是,我没有进行sample program次运行。输入以下内容后,

$ g++ DisplayImage.cpp

出现这些错误,

/tmp/cc3GTOtQ.o: In function `main':
DisplayImage.cpp:(.text+0x53): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0xe5): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0x113): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text+0x147): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
DisplayImage.cpp:(.text+0x169): undefined reference to `cv::waitKey(int)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::~Mat()':
DisplayImage.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::operator=(cv::Mat const&)':
DisplayImage.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::release()':
DisplayImage.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status

2 个答案:

答案 0 :(得分:2)

这是使用OpenCV Libraries的完整命令行构建C ++程序。

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binaryName  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy

如果您在不同的位置安装了OpenCV,则应将“include”和“lib”目录的路径更改为适当的位置。

修改: -

不是每次只在项目目录中创建文件build.sh时运行此命令,而是通过运行chmod 777 build.sh将其权限更改为可执行文件,并且只需运行此文件即可构建项目。

答案 1 :(得分:0)

您需要链接opencv库:您可能需要在命令行中添加至少-lcv-lcxcore以及-lhighgui。请参阅此similar question