对 cv::Mat::Mat() 的未定义引用

发布于 2025-01-20 00:03:10 字数 632 浏览 0 评论 0原文

我编写了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-01-27 00:03:10

这适用于我的 Linux:

g++ main.cpp -I/usr/include/opencv4/ -lopencv_core -lopencv_videoio -lopencv_highgui

虽然这不是可移植的(使用 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。

了解如何自动化此操作,例如通过 MakefileCMakeLists.txt 或只是一个简单的 bash 脚本。

This works on my Linux:

g++ main.cpp -I/usr/include/opencv4/ -lopencv_core -lopencv_videoio -lopencv_highgui

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 name libopencv_core.* or libopencv_mat.*. Find whichever is in your machine (e.g. inside /user/lib) and link it, omitting the extension and the beginning lib 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文