Ask Your Question
0

How can I link the opencv libraries correctly?

asked 2018-04-17 12:07:13 -0600

Imago gravatar image

updated 2018-04-17 12:15:37 -0600

I would like to work with the pre-built version of the opencv libraries in qt creator with either cmake or qmake in windows.

I have already tried various tutorials and asked colleagues at university without much success. However maybe someone sees, what could be wrong and how to fix it. I would be extremely happy, if someone could help me: I updated and use the latest versions qt creator, mingw, cmake and opencv. In "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib" lie the files: "opencv_world341.lib" and "opencv_world341d.lib", they are each about the size: 2.2MB.

I have the CMake File:

cmake_minimum_required(VERSION 2.8)
project(OpenCV_CMake)

set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib")

message("OpenCV_FOUND='${OpenCV_FOUND}'")
find_package( OpenCV REQUIRED )
message("OpenCV_FOUND='${OpenCV_FOUND}'")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} "main.cpp")
message("These are my opencv libs....: '${OpenCV_LIBS}'" )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

And .cpp file:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv) {
  Mat image;
  image = imread(argv[1], 1);
  if (argc != 2 || !image.data) {
    printf("No image data \n");
    return -1;
  }
  namedWindow("Display Image", WINDOW_AUTOSIZE);
  imshow("Display Image", image);
  waitKey(0);
  return 0;
}

cmake compiles properly for me and gives me the output:

Running Windows Runtime device detection.
No winrtrunner.exe found.
Running "C:\cmake\bin\cmake.exe -E server "--pipe=\\.\pipe\{f67b9536-cd8a-43be-8b06-aa9c90087f1d}" --experimental" in C:\myPath\C++\Workingbay\build-CMAKE_TEST-cmake_test-Default.
Running "C:\cmake\bin\cmake.exe -E server "--pipe=\\.\pipe\{250b89c5-6f34-4d67-bfc6-9b0df039ab3b}" --experimental" in C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug.
Starting to parse CMake project.
Starting to parse CMake project.
OpenCV_FOUND=''
Configuring done
OpenCV_FOUND='1'
These are my opencv libs....: 'opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world'
Configuring done
Generating done
Generating done
CMake Project was parsed successfully.
CMake Project was parsed successfully.
Starting to parse CMake project.
OpenCV_FOUND=''
OpenCV_FOUND='1'
These are my opencv libs....: 'opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world'
Configuring done
Generating done
CMake Project was parsed successfully.

However building my project and trying to run the code results in:

18:56:36: Running steps for project OpenCV_CMake...
18:56:36: Starting: "C:\cmake\bin\cmake.exe" --build . --target all
C:\cmake\bin\cmake.exe -HC:\myPath\C++\Workingbay\OpenCV_CMake -BC:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug --check-build-system CMakeFiles\Makefile.cmake 0
C:\cmake\bin\cmake.exe -E cmake_progress_start C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug\CMakeFiles C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/myPath/C++/Workingbay/build-OpenCV_CMake-cmake_test-Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\OpenCV_CMake.dir\build.make CMakeFiles/OpenCV_CMake.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/myPath/C++/Workingbay/build-OpenCV_CMake-cmake_test-Debug'
C:\cmake\bin\cmake.exe -E cmake_depends "MinGW Makefiles" C:\myPath\C++\Workingbay\OpenCV_CMake C:\myPath\C++\Workingbay\OpenCV_CMake C:\myPath ...
(more)
edit retag flag offensive close merge delete

Comments

It is not set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib") but set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build) and you cannot use vc15 lib with mingw

LBerger gravatar imageLBerger ( 2018-04-17 13:26:35 -0600 )edit

@LBerger, can you please explain what and why? Simply writing "set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build) " makes CMake fail parsing the project. I have not yet tried the other part of your comment, nonetheless, I will try it.

Imago gravatar imageImago ( 2018-04-17 14:42:49 -0600 )edit

In path given to cmake you must have a file OpenCVConfig.cmake. I think you can remove this line : a good example.
If cmake cannot find opencv in cmake gui you can give path where is file OpenCVConfig.cmake ou use -DOpenCV_DIR=path_to_OpenCVConfig.cmake

LBerger gravatar imageLBerger ( 2018-04-17 14:54:41 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2018-04-18 01:43:33 -0600

berak gravatar image

you CANNOT use the prebuilt libs with mingw, those are for VS ONLY.

if you want to use mingw, the 1st step would be, to build your own opencv libs locally, using cmake.

cd opencv\build
cmake -DENABLE_PRECOMPILED_HEADERS=OFF -DCPU_DISPATCH="" ..
mingw32-make install

after that, your libs/headers should appear in opencv\build\install .

to compile your project, either use cmake again, and set OpencCV_DIR to opencv\build\install or try with a simple cmdline:

g++ myprog.cpp -Iopencv/build/install/include -Lopencv/build/install/lib/x64/mingw -lopencv_core400 -lopencv_imgproc400 -lopencv_dnn400 -o myprog
edit flag offensive delete link more

Comments

Ok, what do I do, if don't necessarily have to use MinGW, can I use cygwin, clang, .. ? (in fact provisionally set them up)

Imago gravatar imageImago ( 2018-04-18 03:41:48 -0600 )edit

that would not change much. either use visual studio (and the prebuilt libs) or build from src.

(also, support for clang or cygwin is non-existent, you're all on your own, then)

berak gravatar imageberak ( 2018-04-18 04:01:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-17 12:07:13 -0600

Seen: 18,095 times

Last updated: Apr 18 '18