对 cv::Mat::Mat() 的未定义引用
我编写了一个简单的 C++ 代码来读取网络摄像头图像并显示它。但是,当我编译时,出现错误 - '对 cv::Mat::Mat() 的未定义引用'。我不知道为什么它显示两个垫子。这是我的代码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#include <stdlib.h>
int main() {
cv::VideoCapture cap(0);
if (!cap.isOpened){
std::cout << "Error opening camera" << std::endl;
}
cv::Mat img;
while(1){
cap >> img;
cv::imshow("output", img);
cv::waitKey(1);
}
}
这就是我编译它的方式
g++ example.cpp `pkg-config --libs opencv4`
我无法弄清楚为什么会出现错误。任何帮助表示赞赏!
I made a simple c++ code that reads the webcam image and display it. However, when I compile, I get the error - 'Undefined reference to cv::Mat::Mat()'. I don't know why it shows two Mat's. Here is my code:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#include <stdlib.h>
int main() {
cv::VideoCapture cap(0);
if (!cap.isOpened){
std::cout << "Error opening camera" << std::endl;
}
cv::Mat img;
while(1){
cap >> img;
cv::imshow("output", img);
cv::waitKey(1);
}
}
This is how I compile it
g++ example.cpp `pkg-config --libs opencv4`
I can't figure out why the error shows up. Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这适用于我的 Linux:
虽然这不是可移植的(使用
cmake
可以做到这一点,但您需要先学习 cmake),我将给您一个提示,告诉您如何发现它你自己。每当您看到诸如
Undefined reference to cv::Mat::Mat()
之类的错误时,请参阅 https://docs.opencv.org/ ,选择最新版本(此处:https://docs.opencv.org/4.5.5/ ),在“搜索”窗口中输入链接器找不到的类/函数的名称(此处:< code>Mat),读取定义它的标头(此处:#include
),然后缺少的库将具有名称libopencv_core.*
或libopencv_mat.*
。找到您机器中的任何一个(例如在/user/lib
内)并链接它,省略扩展名和名称中开头的lib
。在我的例子中,具有完整路径的库位置是/usr/lib/libopencv_core.so
,因此我将其与-lopencv_core
链接。然后需要用同样的方法找到剩下的lib。了解如何自动化此操作,例如通过
Makefile
、CMakeLists.txt
或只是一个简单的 bash 脚本。This works on my Linux:
While this is not portable, (using
cmake
would do the trick, but you'd need to learn cmake first), I'll give you a hint how you can discover this yourself.Whenever you see an error like
Undefined reference to cv::Mat::Mat()
, go to the documentation at https://docs.opencv.org/ , chose the newest version (here: https://docs.opencv.org/4.5.5/ ), enter, in the "search" window, the name of a class/function the linker cannot find (here:Mat
), read the header that defines it (here:#include<opencv2/core/mat.hpp>
), then the missing library will have the namelibopencv_core.*
orlibopencv_mat.*
. Find whichever is in your machine (e.g. inside/user/lib
) and link it, omitting the extension and the beginninglib
in the name. In my case the library location, with the full path, is/usr/lib/libopencv_core.so
, so I link it with-lopencv_core
. Then you need to find the remaining libs in the same way.Learn how to automatize this, e.g. via a
Makefile
,CMakeLists.txt
or just a simple bash script.