Make OpenCV Work with g++/premake5

I can’t seem to get opencv to work with g++/premake5. Include files work of course, but I try to link the opencv world lib to either g++ and premake5, but i just get an undefined reference error.

More specifically, I’m simply trying to make a cv::Mat object and I get undefined reference to cv::Mat::Mat() and cv::Mat::~Mat().

What can I do to resolve this? CMake is a pain to use so I’m trying to avoid that.

os ? compiler ? opencv version ?
can you show us, what you’re trying to do ?

Sorry, I guess I should have included that info first.

System Info
I am using Windows 10. My compiler is g++ 8.1.0 (installed using MinGW I believe, it’s been a while). OpenCV version 4.5.4 (latest I could find yesterday). My build system is premake5 version 5.0.0-alpha16.

Code Info
Directory Structure (before running premake5):

  • main.cpp
  • premake5.lua

Directory Structure (after running premake5 gmake2 and mingw32-make):

  • main.cpp
  • premake5.lua
  • Makefile
  • opencv-first.make
  • bin/
  • obj/

(last two just store object and binary files)

main.cpp

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


int main() {
    cv::Mat img;
}

premake5.lua (setup build system)
I tried putting in the only lib files I saw, opencv_world454 and opencv_world454d.

workspace "openCV-first"
    configurations {"Debug", "Release"}

project "opencv-first"
    kind "ConsoleApp"
    language "C++"
    targetdir "bin"
    libdirs {"C:\\libraries\\opencv\\build\\x64\\vc15\\lib"}
    includedirs {"C:\\libraries\\opencv\\build\\include"}
    links {"opencv_world454", "opencv_world454d"}

    files {"**.hpp", "**.cpp"}

    filter "configurations:Debug"
        defines {"DEBUG"}
        symbols "On"
    
    filter "configurations:Release"
        defines {"NDEBUG"}
        optimize "On"

After creating these files, I run the following commands with the following output:
premake5 gmake2

Building configurations...
Running action 'gmake2'...
Done (215ms).

mingw32-make

"==== Building opencv-first (debug) ===="
Linking opencv-first
obj/Debug/main.o: In function `main':
C:\Users\pmist\Documents\Projects\opencv\opencv-first/main.cpp:6: undefined reference to `cv::Mat::Mat()'
C:\Users\pmist\Documents\Projects\opencv\opencv-first/main.cpp:6: undefined reference to `cv::Mat::~Mat()'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [opencv-first.make:76: bin/opencv-first.exe] Error 1
mingw32-make: *** [Makefile:30: opencv-first] Error 2

Can you tell if I’m missing any info? Thanks!

1 Like

well, bad news is: you cannot use the prebuilt libs from the winpack,
those are for msvc only (that’s why it refuses to link)
means, you have to build the opencv libs from src, using cmake (hate it or not)

then, later, to build your own project, you can use, whatever ide / tools you like.

1 Like

So basically, I need to use CMake to make my own lib files? Do the include files get generated in the process? (I checked the opencv source files, and include only has opencv.hpp) What if I use the linux build (lol), as they are built with make in mind? I’m guessing they make kernel calls that aren’t supported in windows.

yes

the install process will copy them from the resp. module folders to install/include/opencv2

it’s the same process with mingw, you just have to use mingw32-make instead

1 Like

Alright, I’ve run CMake and mingw32-make and mingw32-make install. The include files get generated and I get a bunch of lib files generated. However, when I try to link the files to premake5 (using same configuration as above, except with new libs), I still get undefined reference to cv::Mat::Mat() error (exact same as above).

Here are the libs generated:

Do you know what could be going wrong?

Lol I was just using the wrong lib name. I should not have included the .dll part of the name. OpenCV seems to work now. Next step will be figuring out how to not use DLLs. Thanks for all your help!

again, it follows gcc convention, if you’re using mingw, like -lopencv_core454

did you mean: building & linking static libs ?
there’s a BUILD_SHARED_LIBS=OFF cmake flag for this.
however, note, that linking your project later will be much more difficult, as you have to order the libs you link to by dependancy, and have to manually add all system libs used

or maybe stay with dyamic and use a single “world” lib (like in your initial try) ,
BUILD_opencv_world=ON would do that.