[Qt-interest] CMake 2.8.6 includes built-in moc support

Stephen Kelly steveire at gmail.com
Wed Oct 5 02:40:48 CEST 2011


Hi,

If you're interested in CMake you will be interested to know that it now 
includes built-in moc support as of todays release.

http://www.kitware.com/blog/home/post/176

http://cmake.org/cmake/help/cmake-2-8-docs.html#prop_tgt:AUTOMOC

The short version is that you can now use CMake for Qt libraries and 
executables like this:

project(myproject)

find_package(Qt4)
include(${QT_USE_FILE})

# Enable built in moc
set(CMAKE_AUTOMOC True)

add_library(foo_library foo.cpp)
target_link_libraries(foo_library ${QT_QTCORE_LIBRARY})

add_executable(bar_executable bar_main.cpp)
target_link_libraries(bar_executable foo_library ${QT_QTGUI_LIBRARY})


... As well as CMake's existing features of course, which I'm not drawing 
attention to. 

The Magic is in the set(CMAKE_AUTOMOC True) which tells CMake to determine 
whether to run moc on source files (the headers for foo.cpp and 
bar_main.cpp, as well as those files themselves), and then runs moc on those 
files whenever necessary.

This is vastly superior to qmake moc support in several ways. If you add a 
Q_OBJECT macro in a qmake based project for example, you have to remember to 
run qmake again or you get "undefined reference to 'vtable for MyObject'". 
CMake automatically runs moc again if needed, so you get that no more.

It is also on par with qmake in making the use of Q_PRIVATE_SLOT and the use 
of the Q_OBJECT macro in cpp files managable. qmake can do this too, but 
this is an improvement over previous versions of CMake. You can also 
#include the moc file in the cpp file or not. CMake will work either way.

I didn't do any of this work. The code has been in KDE for years, written by 
Matthias Kretz of Phonon fame, ported from Qt to STL by Gregory Schlomoff, 
and integrated into CMake by Alexander Neundorf.

I figured anyone who has used CMake for Qt projects would be interested to 
know about this feature. If you don't use CMake then, well, carry on! :)

Thanks,

Steve.







More information about the Qt-interest-old mailing list