PDA

View Full Version : How build Qt with CMake & MinGW in windows?



jennal
10th December 2014, 09:31
I make a test project, and uploaded to github: https://github.com/Jennal/QtCmake

It works fine in MacOSX, but failed to link in Windows.

Errors are:

Linking CXX executable HelloCMakeQt.exe
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x91): undefined reference to `_imp___ZNK7QWidget4sizeEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x22d): undefined reference to `_imp___ZNK11QMouseEvent3posEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x27f): undefined reference to `_imp___ZNK11QMouseEvent1xEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x28c): undefined reference to `_imp___ZNK6QPoint1xEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x2a5): undefined reference to `_imp___ZNK11QMouseEvent1yEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x2b2): undefined reference to `_imp___ZNK6QPoint1yEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x2cb): undefined reference to `_imp___ZNK11QMouseEvent7buttonsEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x351): undefined reference to `_imp___ZNK11QMouseEvent7buttonsEv'
CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj):(.text+0x400): undefined reference to `_imp___ZNK11QMouseEvent3posEv'
C:\msys64\mingw32\bin\ld.exe: CMakeFiles\HelloCMakeQt.dir/objects.a(main.cpp.obj): bad reloc address 0x8 in section `.text[__ZN10MainWindowD2Ev]'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles\HelloCMakeQt.dir\build.make:295: recipe for target 'HelloCMakeQt.exe' failed
mingw32-make[2]: *** [HelloCMakeQt.exe] Error 1
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/HelloCMakeQt.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/HelloCMakeQt.dir/all] Error 2
Makefile:136: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

I googled for days and can't find any solution. Can someone help me?

d_stranz
12th December 2014, 20:29
You may have to add a preprocessor define "QT_DLL" to tell the compiler and linker that you are using Qt libraries as DLLs. This causes the Windows-specific __declspec( dllimport ) and __declspec( dllexport ) macros to be defined, which in turn modifies the mangled names of the classes and methods in the Qt header files so they match the exports from the DLLs.

I am not familiar enough with CMake to tell you how to add an environment-specific preprocessor define.

jennal
13th December 2014, 04:00
Thank you for your reply.

I found that failed because I compile some library with STATIC option, and Qt is linked with SHARED by default. Then I download Qt source code and compile to STATIC with help of the document: http://qt-project.org/wiki/How-to-build-a-static-Qt-for-Windows-MinGW

And I success to build the exe file. But still failed to launch. When I launch the exe, it shows an error message:



This application failed to start because it could not find or load the Qt platform plugin "windows".

Reinstalling the application may fix this problem.


Then a runtime error message:



Runtime Error!

Program:
C:\Path\HelloQt.exe

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


Any idea of this issue?

wysota
13th December 2014, 09:07
Qt needs a couple of plugins to work. If you use Qt built statically, you need to build the plugins statically and link those plugins explicitly with your application. The docs page on "Static Plugins" should help you with that.

jennal
15th December 2014, 01:19
@wysota Thank you for your help. I will solve this by myself then.