Qt AUTOMOC with CMake


Configuring big Qt projects with CMake could be sometimes annoying because of the necessity to apply the moc command over those specific header files containing the Qt Q_OBJECT macro. Long time ago the CMake macro qt4_wrap_cpp was introduced to help in the preprocessing of those files and it helps a lot. However, in big projects where we have header files containing Q_OBJECT macro and other ones where the macro is not included we have to specify the first ones in a list of files that will be preprocessed with the qt4_wrap_cpp macro. An Cmake configuration example of what I’m talking about could be:
SET(COMMON_SRC file1.cpp file2.cpp file3.cpp)
SET(COMMON_HDR file1.h file3.h)
SET(COMMON_UIS file1.ui file3.ui)
QT4_WRAP_CPP(COMMON_MOC ${COMMON_HDR})
QT4_WRAP_UI(COMMON_UIS ${COMMON_UIS})
ADD_LIBRARY(project_common ${COMMON_SRC} ${COMMON_MOC} ${COMMON_UIS})
But things could be easier. This afternoon I was watching some of the videos of the Qt Developers day in Berlin 2012 (http://www.cyberhades.com/2012/12/13/videos-del-qt-developers-day-berlin-2012/). Between them, I watched the presentation of Stephen Kelly talking about how to configure Qt projects with CMake in Qt4 and Qt5 (Some interesting new features will come soon for the new release of Qt ^^). In his presentation Stephen mentioned a new feature introduced in CMake 2.8.8 that let us avoid to apply the qt4_wrap_cpp macro over those headers containing Q_OBJECT macro. It’s so simple as setting the CMAKE_AUTOMOC variable to ON. Here you have the new version of the previous code using CMAKE_AUTOMOC feature.
SET(CMAKE_AUTOMOC ON)
...
SET(COMMON_SRC file1.cpp file2.cpp file3.cpp)
SET(COMMON_UIS file1.ui file3.ui)
QT4_WRAP_UI(COMMON_UIS ${COMMON_UIS})
ADD_LIBRARY(project_common ${COMMON_SRC} ${COMMON_UIS})
loading...





Últimos comentarios