colaj
colaj

Reputation: 11

Undefined references to OpenCV functions?

I have a simple C++ code 'myscript.cpp':

#include <iostream>
#include <opencv2\opencv.hpp>

using namespace std;

int main(){
    cout << "hello everyone" << endl;
    
    // Read an image
    cv::Mat img_test = cv::imread("test.jpg", 0);

    return 0;
}

...and I try to build an .exe using this command out of a .bat script:

g++ -fdiagnostics-color=always -Wall -Wextra -o output.exe myscript.cpp -I C:\CPP\opencv\build\include -L C:\CPP\opencv\build\x64\vc16\lib -l opencv_world490

The opencv library I simply got from opencv-4.9.0-windows.exe downloaded from the official https://opencv.org/releases. As far as I understand it, the compiler/linker is able to find all the given directories and paths, but it does not seem to be able to extract the function definitions. I tried the same using other libraries such as glfw. The error messages read

C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\name\AppData\Local\Temp\cc1JAyEn.o:myscript.cpp:(.text+0x7a): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\name\AppData\Local\Temp\cc1JAyEn.o:myscript.cpp:(.text+0xd2): undefined reference to `cv::Mat::~Mat()'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\name\AppData\Local\Temp\cc1JAyEn.o:myscript.cpp:(.text+0x114): undefined reference to `cv::Mat::~Mat()'
collect2.exe: error: ld returned 1 exit status

How can I fix this? (why is it giving these weird relative paths?) I am used to do it in a similar fashion on Linux but it seems on Windows every C++ discussion is related to Visual Studio but I cannot use it because of license reasons (VSCode is possible though).

Upvotes: 1

Views: 45

Answers (0)

Related Questions