我最近在Ubuntu 20.04上(重新)安装了OpenCV 4.0.0,但是当我尝试在Sublime Text上构建一些c ++代码时,它给了我错误。
这是代码:
describe('Creating', () => {
it('saves a user', () => {
const testUser = new User({ name: 'Test' });
testUser.save();
});
});
这是输出:
#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;
};
我知道这是“未定义的引用”类型的错误,但是,作为c ++菜鸟,我不知道如何解决该问题。
编辑(已解决):好的,所以问题出在Sublime Text 3用于C ++的构建系统中。
按照Thomas Sablik的建议,我查找了默认构建系统中使用的命令,并向其中添加了以下代码:/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]
。为了简化整个过程并避免将来出现问题,最好创建另一个构建系统(`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}\""
}
]
}