In Qt5, writing Cmake project for building Qt5 opengl app with optional shader seems to be little complicated comparing to previous Qt version(i.e Qt v5.0.2) and took time to figure out how to configure all necessary things. After many hours on this issue, I found one configuration to make possible to building Qt5 opengl app with Cmake and this post is to describe what I did with helps from various internet sources which will be listed below accordingly.


First thing to tell is preconditions before writing sources.


Precondition


1. OS: MS Windows 7

2. Qt version and toolkit: prebuilt Qt 5.1.0 msvc2010_opengl

3. Visual studio 2010

4. Windows SDK should be installed properly. : ie) c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\

5. CMake 2.8.11


If your system is different with the above precondition, CMakeLists.txt should be changed properly. Refer to the example CMakeLists.txt in this post. You can get a hint about how to edit.


Now it is time to introduce source code for building a simple Qt5 opengl shader app. 


Because main purpose to write up this post is to show how to create CMakeLists.txt to compile existing source codes, just copy and create all 3 files referring to this link:  http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html


And last file CMakeLists.txt.


CMakeLists.txt


Now we have all files to build up Qt5 shader app showing rotating triangle and time to run cmake to create visual studio project file for this app. If you are lucky to complete compilation and launching this app, then you will see a rotating triangle with help of Opengl shader in a window.



One last thing I want to note is that CMAKE_PREFIX_PATH in CMakeLists.txt MUST include proper paths in order to succeed compilation. As you can see in the above example, there are two paths are included.

First path is the location to find all neccessary Qt5 tools and must be same with the path in below picture. Below picture shows Qt Options dialog box in Visual studio 2010 through "Qt5" -> "Qt5 options" which is enabled if you installed visual studio addin for Qt5





Second path in CMAKE_PREFIX_PATH is required to compile with OpenGL library: glu32.lib and opengl32.lib. These files are included in Windows SDK so make sure this before starting compilation and also don't forget to include TARGET_LINK_LIBRARIES in the above example. Otherwise, opengl libraries will not be included in the project file generated by cmake.


Added 2013-09-23.


You may notice below error message on compilation.

Error message=> module machine type 'x64' conflicts with target machine type 'X86'


This error message happens when you create visual studio project file for 32 bit binary and try to compile against 64 bit library. The default behavior of running "cmake .." on 64 bit machine usually generates 32 bit project instead of 64 bit project file so this may cause compilation error.


Solution. Try to generate 64 bit visual studio project file using below command and compile binary with it.

cmake -G "Visual Studio 11 Win64" ..


Below is source code for your convenience.


qt5shader.7z


Posted by kevino
,