Opencv: How to import automatic lib

Asked

Viewed 113 times

2

Good morning, I’m starting programming with Opencv in C++ with Mingw, but I’m having trouble with lib files. Every time I need to compile a program, I need to put, for example, -llibopencv_videoio330. I wonder if there’s any way to make it automatic. I’ve already created a variable LIBRARY_PATH, but it doesn’t seem to have worked.

1 answer

1

There are several ways. You can create a script file (Batch on Windows, Bash on Linux, for example) to automate this build. You can also use a Makefile. But I suggest you use a configuration manager like the Cmake to generate the project files you will use on the appropriate platform and compiler.

When creating the Cmake file there is a command called find_package that "magically" finds the dependencies of a library that also exposes a "Cmake interface" (as is the case with Opencv). And there’s also a command called target_link_libraries that does exactly what you want: link the right libraries. Soon, you can do something more or less like this in your Cmake script:

. . .
find_package(OpenCV REQUIRED core highgui imgproc)
include_directories(${OpenCV_INCLUDE_DIRS})
. . .
target_link_libraries(MeuProjeto ${OpenCV_LIBS})
. . .

And then, when you generate the project (that is, the Visual Studio project, if you use this compiler, or the Makefile if you use any other), Cmake will alone find where the Opencv libraries are and reference in your project. And when you compile later, with this generated project, everything will work transparently.

Cmake has a boring learning curve (especially because the documentation isn’t all that, in my opinion), but it’s worth it. If it’ll help you, this project of mine at Github uses Opencv in C++ and employs Cmake to do exactly what I suggested above. Have a look at the file CMakeLists.txt at the root of the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.