CMake, Visual Studio and Qt 5 with MOC...

After building a 64-bit version of Qt with/for Microsoft Visual Studio, building a simple Qt test program and doing first steps with the CMake build system, the next hurdle appeared on the horizon in the form of support for Qt and its Q_OBJECT features (I am currently fighting more with the changes I made to my build environment than improving the actual program code…).

So, a looonng time of reading, testing, failing and cursing later, the following seems the way to go for my setup:

Tell CMake where to find the Qt files by adding this to the CMakeLists.txt; note that you have to be very specific where it should look for those Qt5Config.cmake files; I first tried C:/devel/ext/Qt/5.2.1/_64DLL/ — no luck:

set (CMAKE_PREFIX_PATH C:/devel/ext/Qt/5.2.1/_64DLL/lib/cmake)

CMAKE_AUTOMOC is a new feature since CMake 2.8.6, so that the Qt Meta Object Compiler (MOC) will automatically be called for those targets with Q_OBJECT/signals/slots.



With that being set, the required moc_*.cpp and *.moc files will be generated by CMake in the build directory; you don’t have to put them in the project’s original source code directory or add them to the file list in the CMakeLists.txt.

You then still have to modify your source code files, depending on where Q_OBJECT is used: