1

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}\""
        }
    ]
}
Xartab
  • 37
  • 1
  • 7
  • 1
    Do you link opencv into your project? How do you build your project? – Thomas Sablik Jul 18 '20 at 12:17
  • I'm not exactly sure what that first bit means, but I build the project using Sublime Text 3's default c++ build-system. – Xartab Jul 18 '20 at 13:41
  • 1
    You could use OpenCV with a package manager like [Conan](https://conan.io/) and a build system like [CMake](https://cmake.org/) or you could build your project with: `g++ "/home/user/Projects/Mind/CScript/Script.cpp" -o "/home/user/Projects/Mind/CScript/Script" \`pkg-config --cflags opencv\` \`pkg-config --libs opencv\`` – Thomas Sablik Jul 18 '20 at 14:46
  • The compiler is g++ 9.3.0. I didn't add nor remove any flags (and to be quite frank I wouldn't know where to begin doing that) – Xartab Jul 18 '20 at 14:52
  • 1
    Did you try to compile the project with the line `g++ "/home/user/Projects/Mind/CScript/Script.cpp" -o "/home/user/Projects/Mind/CScript/Script" \`pkg-config --cflags opencv\` \`pkg-config --libs opencv\``? – Thomas Sablik Jul 18 '20 at 14:53
  • I did. It gives back no error in the terminal, but it's still the same in ST. – Xartab Jul 18 '20 at 14:56
  • 1
    Did you change the build properties? What is the value of `cmd`? – Thomas Sablik Jul 18 '20 at 14:58
  • I didn't touch the build-system, if that's what you're asking. I'm trying to find the file to check, but I don't know where to look and am trying to google the location in the meantime. Also, sorry for being so useless in trying to help you help me. – Xartab Jul 18 '20 at 15:06
  • 1
    https://www.sublimetext.com/docs/3/build_systems.html You have to configure your build system. You want to use an external library. You have to set the include paths and the library paths so the compiler and linker can find the necessary files. – Thomas Sablik Jul 18 '20 at 15:07
  • Found it! There are two: `"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\""` and `shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""`. The second one is under "variants" – Xartab Jul 18 '20 at 15:09
  • 1
    It's working! Thank you kind sir, you are a gentleman and a scholar! – Xartab Jul 18 '20 at 15:14
  • I reopened the question. Please turn your solution into an answer so other people can benefit from it (and don't have to try to decipher the code in the comment. Thanks! – MattDMo Jul 19 '20 at 15:33

0 Answers0