首页 > 解决方案 > opencv 在 ubuntu 20.04 上不适用于 c++

问题描述

我最近(重新)在 Ubuntu 20.04 上安装了 OpenCV 4.0.0,但是当我尝试在 Sublime Text 上构建一些 c++ 代码时,它给了我错误。

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int radius = 50;
int curve_angle = 10;

int main()
{
    Mat img(radius, radius, CV_8UC3, Scalar(0,0, 100));
    
    namedWindow("showWindow", WINDOW_AUTOSIZE);
    imshow("showWindow", img);

    waitKey(0);
    destroyWindow("showWindow");

    return 0;
};

这是输出:

/usr/bin/ld: /tmp/ccUe3T91.o: in function `main':
Script.cpp:(.text+0xa9): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: Script.cpp:(.text+0x122): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: Script.cpp:(.text+0x159): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: Script.cpp:(.text+0x194): undefined reference to `cv::destroyWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccUe3T91.o: in function `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)':
Script.cpp:(.text._ZN2cv3MatC2EiiiRKNS_7Scalar_IdEE[_ZN2cv3MatC5EiiiRKNS_7Scalar_IdEE]+0xe4): undefined reference to `cv::Mat::operator=(cv::Scalar_<double> const&)'
/usr/bin/ld: /tmp/ccUe3T91.o: in function `cv::Mat::~Mat()':
Script.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3d): undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/ccUe3T91.o: in function `cv::Mat::create(int, int, int)':
Script.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0xa1): undefined reference to `cv::Mat::create(int, int const*, int)'
/usr/bin/ld: /tmp/ccUe3T91.o: in function `cv::Mat::release()':
Script.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4f): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
[Finished in 6.3s with exit code 1]
[shell_cmd: g++ "/home/user/Projects/Mind/CScript/Script.cpp" -o "/home/user/Projects/Mind/CScript/Script" && "/home/user/Projects/Mind/CScript/Script"]
[dir: /home/user/Projects/Mind/CScript]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

我知道这是一个“未定义的引用”类型的错误,但是,作为一个 c++ 菜鸟,我不知道如何解决这个问题。




编辑(已解决):好的,所以问题在于 Sublime Text 3 用于 C++ 的构建系统。按照Thomas Sablik的建议,我查找了用于默认构建系统的命令,并在其中添加了以下代码:`pkg-config --cflags opencv` `pkg-config --libs opencv`. 为了使整个过程更容易并避免将来出现问题,最好只创建另一个构建系统(Tools > Build System > New Build System)。

这是默认代码:

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

这是我现在使用的构建系统:

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" `pkg-config --cflags opencv` `pkg-config --libs opencv` && echo 'Build complete'",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" `pkg-config --cflags opencv` `pkg-config --libs opencv` && echo 'Build complete' && \"${file_path}/${file_base_name}\""
        }
    ]
}

标签: c++opencvsublimetext3ubuntu-20.04

解决方案


推荐阅读