来自终端的示例程序无法运行

Posted

技术标签:

【中文标题】来自终端的示例程序无法运行【英文标题】:Sample Program From Terminal Could Not Run 【发布时间】:2013-11-11 05:59:30 【问题描述】:

我是 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

【问题讨论】:

【参考方案1】:

这是带有 OpenCV 库的完整命令行构建 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 将其更改为可执行权限,而构建项目只需运行此文件即可。

【讨论】:

嗨。这工作得很好。每次创建新项目时都必须输入所有这些吗? 您可以创建一个包含此内容的文件,并将其权限更改为可执行,每当您需要构建项目时运行它。【参考方案2】:

您需要与 opencv 库链接:您可能需要在命令行中添加至少 -lcv-lcxcore-lhighgui。看到这个similar question。

【讨论】:

还是不行。为了您的信息,我在桌面上创建了一个文件夹,.cpp 位于那里。我以前在 VS2008 中使用它,我需要链接库、环境变量并包含依赖项。我想尝试从终端编译,但我不能。

以上是关于来自终端的示例程序无法运行的主要内容,如果未能解决你的问题,请参考以下文章

无法通过单击来运行程序,而我可以从终端

终端里为啥无法运行python?

如何使用swift从Xcode coco app运行像FFMPEG这样的终端程序?

无法运行 Spring Boot 应用程序?

无法运行示例 Java EE 程序

无法运行颤振项目