问题
I've recently (re-)installed OpenCV 4.0.0 on Ubuntu 20.04, but when I try to build some c++ code on Sublime Text, it gives me back errors.
This is the code:
#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;
};
and this is the output:
/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]
I understand that this is an "undefined reference" type of error, but, as a c++ noob, I have no idea how to approach the problem.
EDIT(SOLVED): Ok, so the problem lay in the build system that Sublime Text 3 uses for C++.
Following Thomas Sablik's advice, I looked for the command used into the default build system, and added this bit of code to it: `pkg-config --cflags opencv` `pkg-config --libs opencv`
. To make the whole process easier and avoid problems in future, it's better to just create another build system (Tools > Build System > New Build System
).
This is the default code:
{
"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}\""
}
]
}
And this is the build system I'm using now:
{
"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}\""
}
]
}
来源:https://stackoverflow.com/questions/62968443/opencv-not-working-for-c-on-ubuntu-20-04