Search code examples
c++qtmoc

How to generate moc_*.cpp files using CMake


Suppose I have a C++ file named animals.cpp. How can I generate a moc file (moc_animals.cpp) using CMake?


Solution

  • There are multiple ways. if you are using CMAKE version grater than 2.8.6 then you can use AUTOMOC which should do everything automatically for you.

    Here there is a nice tutorial.

    As pointed out by @spectras this assumes respecting their naming convention. If you want to use your specific naming you can of course use the manual mechanism by simple calling qt4_wrap_cpp or qt5_wrap_cpp. This is also what you should do in case you are bound to older CMAKE versions.