c++ - 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}\""
}
]
}
解决方案
推荐阅读
- python - 尝试格式化时间和日期时,“模块‘babel’没有属性‘日期’”
- c# - 基于声明的授权
- java - 如何在 ListView 中的项目上显示日期?
- database - 托管环境上的 Cakephp 4 MySQL
- java - 如何获取日期的UTC日期?
- asp.net - ASP.NET 5.0 在另一个程序集上查找控制器
- deployment - jProfiler - 需要通过 UCD 部署 linux 代理
- javascript - Cheerio 数据扩散混乱
- python - 如何使用python获取图像的像素坐标
- python - 检查 list_1 中的元素是否匹配 dict.key 和 list_2 中的元素是否匹配 dict.value