Opencv: How to import automatic lib

2

Good morning, I'm starting to program with OpenCV in C ++ with MinGW, but I'm having difficulty with lib files. Every time I need to compile a program, I need to put, for example, -llibopencv_videoio330 . I would like to know if there is any way to make this automatic. I already created a LIBRARY_PATH variable, but it does not seem to work.

    
asked by anonymous 08.09.2017 / 15:57

1 answer

1

There are several ways. You can create a script file (Batch in Windows, Bash in Linux, for example) to automate this compilation. You can also use a Makefile . But I suggest you use a configuration manager like CMake to generate the project files that you will use in 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 expose 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: linka the right libraries. So you can do something like this in your CMake script:

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

Then, when you generate the project (that is, the Visual Studio project, if you use this compiler, or the Makefile if you use some other), CMake will only find where the OpenCV libraries are and reference in your project. And when you compile later, with this project generated, everything will work transparently.

CMake has a boring learning curve (especially since the documentation is not there, in my opinion), but it's worth it. If it will help, this my project in Github uses OpenCV in C ++ and employs CMake to do exactly what I suggested above. Take a look at the CMakeLists.txt file at the root of the project.

    
15.09.2017 / 03:57