Search code examples
qtqt4cmakemoc

CMake + Qt : define the moc/ui output directory


I'm currently transferring a project built with qmake to CMake.

In the version with qmake, in the .pri file, there was

MOC_DIR = .moc/$${PLATFORM_NAME}

that permitted to produce the MOC temporary files in a given directory, keeping the sources clean. How to do the same thing with CMake?

Note: with CMake, I use the FindQt4.cmake package and the command QT4_WRAP_CPP().


Solution

  • As baysmith says, if your goal is to keep your source directory clean, the real solution is to use CMake's "out-of-source" builds feature. If you're on Windows, set "Where to build the binaries" to a new directory, different from the "Where is the source code" directory. If you're on Unix, it goes something like this:

    cd <source directory>
    mkdir build
    cd build
    cmake ..
    make
    

    By running CMake on a different directory, all of the build files will go into that directory, and your sources will stay clean. (Note: the build directory doesn't have to be inside the source directory. See the CMake wiki for more details.)

    If "out-of-source" doesn't work for you, I was able to find one other option. Based on what I can tell from the Qt4Macros.cmake file installed with my CMake 2.8, it isn't accessible as a config parameter. Here's the relevant line:

    SET(_moc    ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
    

    The workaround is to change all of your MOC include directives to specify the subfolder you'd like to build to.

    #include "moc/mainwindow.moc"
    

    After creating the moc directory inside my build directory, there were no problems building, and my MOC file was in the new directory.