Problems building OpenCV for Qt using cmake in Windows

What I want is to build OpenCV for Qt using cmake in Windows, but there were some annoying problems.

The version info:
Qt 5.12.10
cmake 3.20.0
MinGW 7.3.0(Embeded in Qt)
OpenCV 4.5.2 and 3.5.14(I has tried two different versions, but got the similar errors)

In cmake, I set the following opinions,

  • WITH_QT***************************************checked
  • WITH_OPENGL********************************checked
  • WITH_IPP *************************************unchecked
  • OPENCV_ENABLE_ALLOCATOR_STATA**unchecked
  • Qt5Concurrent_DIR**D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5Concurrent
  • Qt5Widgets_DIR******D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5Widgets
  • Qt5Test_DIR***********D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5Test
  • Qt5OpenGL_DIR******D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5OpenGL
  • Qt5Gui_DIR************D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5Gui
  • Qt5Core_DIR***********D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5Core
  • Qt5_DIR****************D:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake/Qt5

I has run

  • configure
  • generate

successfully in cmake, but I met errors when I run mingw32-make -j 8, the error is

[ 42%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/merge.avx2.cpp.obj
Building CXX object modules/core/CMakeFiles/opencv_core.dir/mean.avx2.cpp.obj
[ 42%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/sum.avx2.cpp.obj
[ 42%] Building RC object modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj
gcc: error: Files\opencv-3.4.14\sources\modules\core\include: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build\modules\core: No such file or directory
gcc: error: Files\opencv-3.4.14\sources\3rdparty\zlib: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build\3rdparty\zlib: No such file or directory
gcc: error: Files\opencv-3.4.14\sources\3rdparty\include\opencl\1.2: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build: No such file or directory
D:\Qt\Qt5.12.10\Tools\mingw730_64\bin\windres.exe: preprocessing failed.
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1616: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
mingw32-make[2]: *** Waiting for unfinished jobs…
[ 42%] Linking CXX static library …\lib\libIlmImf.a
[ 42%] Built target IlmImf
mingw32-make[1]: *** [CMakeFiles\Makefile2:2004: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2
PS D:\Program Files\opencv-3.4.14\opencv-build>
It says the cmd in build.make:1616 didn’t run successfully, and I found the file, the content in 1616 line was cd /d "D:\Program Files\opencv-3.4.14\opencv-build\modules\core" && D:\Qt\Qt5.12.10\Tools\mingw730_64\bin\windres.exe -O coff $(RC_DEFINES) $(RC_INCLUDES) $(RC_FLAGS) "D:\Program Files\opencv-3.4.14\opencv-build\modules\core\vs_version.rc" CMakeFiles\opencv_core.dir\vs_version.rc.obj

I has searched a lot about this in Google, and I has tried the following two solutions:
1. uncheck OPENCV_ENABLE_ALLOCATOR_STATA
2. delete $(RC_DEFINES) $(RC_INCLUDES) $(RC_FLAGS) in build.make file, because someone said that they are not important. At first, it’s useful, but after I met very many errors similar to this one in many build.make files in other pathes, I still try this solution, but sometimes it was useless. Finally, the process was terminated completely.
Unfortunately, all of them were useless for me.
I don’t know much about cmake and makefile. Maybe that’s why this error has been confusing me for two days. Hope you guys can help me, thank you so so so much.

The tutorials I refered listed here,
https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
In Chinese:
https://blog.csdn.net/weixin_43763292/article/details/112975207

yea, the problem is the windres.exe, that comes with mingw

should be:

uncheck OPENCV_ENABLE_ALLOCATOR_STATS

it alse should be fixed in 4.5.2 are you sure about the version ?

Thanks for reply.

Yeah, it’s OPENCV_ENABLE_ALLOCATOR_STATS, sorry for my typo.
And I am sure that the version is 4.5.2.
image
Honestly, I doubt that what I met is not due to the OPENCV_ENABLE_ALLOCATOR_STATS. I noticed that the error message for that should be like below

gcc: error: long: No such file or directory

But for me it’s

gcc: error: Files\opencv-3.4.14\sources\modules\core\include: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build\modules\core: No such file or directory
gcc: error: Files\opencv-3.4.14\sources\3rdparty\zlib: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build\3rdparty\zlib: No such file or directory
gcc: error: Files\opencv-3.4.14\sources\3rdparty\include\opencl\1.2: No such file or directory
gcc: error: Files\opencv-3.4.14\opencv-build: No such file or directory

It looks like a error about pathes, thouth the mentioned pathes are there indeed.

that’s the mentioned trouble with mingw’s windres.exe. it has to parse a cmdline like

OPENCV_ALLOCATOR_TYPE=long long

and breaks on the space between the 2 long’s (so the 2nd long is interpreted as next argument, a filename), silly thing !

but again, uncheck OPENCV_ENABLE_ALLOCATOR_STATS, clean build folder, and restart with cmake

also try with -j1, not -j8 , will take longer, but it won’t mix output from different threads

Following your advice, I build OpenCV using mingw32-make -j 1 again. It’s wired that I met the same error when it finished only 9%. I am more confused…

please avoid screenshots of errors or code here. (can’t be indexed, copy/pasted, etc)

what’s your directory structure ? it looks weirdly different from the one on github

opencv-build 

does it exist ?

Sorry for that, I’ll avoid that next time.
The dir exists indeed.
And the structure is like this

opencv-4.5.2
├─opencv-build
│ ├─3rdparty
│ ├─apps
│ ├─bin
│ ├─CMakeFiles
│ ├─configured
│ ├─data
│ ├─doc
│ ├─include
│ ├─lib
│ ├─modules
│ ├─opencv2
│ ├─python_loader
│ ├─test-reports
│ ├─tmp
│ └─win-install
└─sources
├─.cache
├─3rdparty
├─apps
├─cmake
├─data
├─doc
├─include
├─modules
├─platforms
└─samples

The last 10 directories belong to /soures, the retracts in the code block is a little unclear.

D:\Program Files\opencv-4.5.2

that’s a terrible place to do this. the blank between “Program” and “Files” breaks it
(it’s looking in “Files\opencv-4.5.2”, which does NOT exist, look at your output, again !)

please try from a different location, copy to a folder without spaces in the path

Great! The space in path is really the reason.
I must note this after.
But there is another problem.
I have not searched it a lot yet. I will post it here. If you know how to solve it, it’s wonderful and I will also try my best to conquer it.

[ 55%] Building CXX object
modules/calib3d/CMakeFiles/opencv_calib3d.dir/src/quadsubpix.cpp.obj
CMakeFiles\opencv_highgui.dir/objects.a(window.cpp.obj):window.cpp:(.text$cvSetWindowProperty+0x27): undefined reference to cvSetPropVsync_W32(char const*, bool)' CMakeFiles\opencv_highgui.dir/objects.a(window.cpp.obj):window.cpp:(.text$cvGetWindowProperty+0x56): undefined reference to cvGetPropVsync_W32(char const*)’
CMakeFiles\opencv_highgui.dir/objects.a(window.cpp.obj):window.cpp:(.text$_ZN2cv17setWindowPropertyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEid+0x5a): undefined reference to cvSetPropVsync_W32(char const*, bool)' CMakeFiles\opencv_highgui.dir/objects.a(window.cpp.obj):window.cpp:(.text$_ZN2cv17getWindowPropertyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi+0xc1): undefined reference to cvGetPropVsync_W32(char const*)’
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [modules\highgui\CMakeFiles\opencv_highgui.dir\build.make:222: bin/libopencv_highgui452.dll] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:2666: modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
mingw32-make[1]: *** Waiting for unfinished jobs…

@Mooki_Xiao , you’ll have to make a decision, do you want to

  • use the Qt enhancements in the highgui module (imshow()), or
  • write your own Qt based ui

you cannot have both in the same program (unless you build static opencv libs, that is) !

if it’s the latter, disable WITH_QT in cmake

also see here: 4.5.2 static library, highgui module compile failure LNK2019 · Issue #19870 · opencv/opencv · GitHub
cc @Crackwitz

To be honest, I don’t know what I want clearly yet. :crazy_face:
I compile OpenCV to meet the requirements of a project I found in GitHub.
The good news is that I has tried the solution in above issue you mentioned, it works.
So I made it, completely!
Thank you so much for taking time to help me. :smiley:

1 Like

nice to hear that, can you close your github issue, please ?

absolutely, it’s closed now. :grinning:

1 Like