# Problems building OpenCV for Qt using cmake in Windows

**URL:** <https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662>\
**Category:** C++\
**Tags:** highgui, qt, windows, build\
**Created:** [April 8, 2021, 4:57am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662 "2021-04-08T04:57:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 4:57am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/1 "2021-04-08T04:57:56Z")

</div>

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](https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows)  
In Chinese:  
[https://blog.csdn.net/weixin\_43763292/article/details/112975207](https://blog.csdn.net/weixin_43763292/article/details/112975207)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 7:42am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/2 "2021-04-08T07:42:58Z")

</div>

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

> [@Mooki\_Xiao](#):
>
> uncheck OPENCV\_ENABLE\_ALLOCATOR\_STATA

should be:

uncheck OPENCV\_ENABLE\_ALLOCATOR\_STATS

it alse [should be fixed in 4.5.2](https://github.com/opencv/opencv/pull/19023) are you sure about the version ?

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 8:06am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/3 "2021-04-08T08:06:29Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/464e4c9af51962a8624d6a17b1a6a3c0218c411e.png)  
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

```auto
gcc: error: long: No such file or directory

```

But for me it’s

```auto
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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 8:14am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/4 "2021-04-08T08:14:32Z")

</div>

> [@Mooki\_Xiao](#):
>
> `gcc: error: long: No such file or directory`

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

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 8:50am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/5 "2021-04-08T08:50:06Z")

</div>

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…

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/65a00780662e4016b2e7754b14a59e30f882dc5b.png)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 8:54am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/6 "2021-04-08T08:54:57Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 9:13am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/7 "2021-04-08T09:13:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 9:22am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/8 "2021-04-08T09:22:39Z")

</div>

`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

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 11:05am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/9 "2021-04-08T11:05:09Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 11:18am UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/10 "2021-04-08T11:18:16Z")

</div>

@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](https://github.com/opencv/opencv/issues/19870)  
cc @Crackwitz

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 2:27pm UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/11 "2021-04-08T14:27:13Z")

</div>

To be honest, I don’t know what I want clearly yet. 🤪  
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. 😃

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 8, 2021, 3:25pm UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/12 "2021-04-08T15:25:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mooki\_Xiao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mooki_xiao/32/1739_2.png) [@Mooki\_Xiao](https://forum.opencv.org/u/Mooki_Xiao)\
**Post date:** [April 8, 2021, 6:37pm UTC](https://forum.opencv.org/t/problems-building-opencv-for-qt-using-cmake-in-windows/2662/13 "2021-04-08T18:37:32Z")

</div>

absolutely, it’s closed now. 😀
