Qt World Summit: Submit your Presentation

Qt + CMake no moc files genereated

  • Hi, I'm trying to build a simple QCoreApplication prorgam using CMake as a project files.
    At time of building I get link error to Qt meta, the ones that shoul be generated with moc.

    I followed the documentation on cmake and qt on how to set up a project, but Automoc does'nt work

    this is my cmake file:

    cmake_minimum_required(VERSION 2.8)
    aux_source_directory(. SRC_LIST)
    find_package(Qt5 COMPONENTS core REQUIRED)
    add_executable(${PROJECT_NAME} ${SRC_LIST})
    target_link_libraries(${PROJECT_NAME} Qt5::Core)

    the source files are a simple helloworld main.cpp file and two files containing a simple QObject
    definition and declaration(just to test the moc generation)

  • Lifetime Qt Champion


    Following Qt's cmake manual you might be missing the set(CMAKE_INCLUDE_CURRENT_DIR ON) line in your CMakeLists.txt.

    Hope it helps

  • Mmmh a great miss, however this was a test I made to figure out why another similar project doesn't work.. And I got the same errors, but in that project I'm pretty sure I didn't miss that line. Now I don't have the code under the eyes, but I'll let you know if it work!

  • Lifetime Qt Champion

    Depending on the size and complexity of the beast, you might have better luck recreating the CMakeLists.txt file from scratch and re-add stuff piece by piece until it either breaks again or just works.

  • i just re-read the first-post and as you can see the


    is already set...

  • Lifetime Qt Champion

    Right, I managed to miss it...

    Which version of CMake and Qt are you using ?

  • Cmake version 3.7.0 and Qt 5.7

  • Lifetime Qt Champion

    Ok, can you share the code you have to reproduce your problem ?

  • of corse! The cmake file is the same i posted in the previous post


    #include <QObject>
    class Test : public QObject
      virtual ~Test() {}


    #include "test.h"


    #include <iostream>
    #include <QCoreApplication>
    #include <QDebug>
    #include "test.h"
    using namespace std;
    int main(int argc, char *argv[])
      QCoreApplication a(argc, argv);
      Test t;
      qDebug() << t.objectName();
      return a.exec();

    instead if you prefer the "real project"(the errors are pretty the same)..
    github repo
    to compile it you need tinyXML2. There is the working .pro file and the non working cmake file.

  • Isn't moc only needed if your QObject class has signals and/or slots?

  • I think it's needed only the Q_OBJECT macro to activate moc, signals and slots are inherited from QObject.

  • Lifetime Qt Champion

    @ceora That's not the problem at hand it's the automoc not running properly.

    @ceora try with:

    cmake_minimum_required(VERSION 2.8.11)
    file(GLOB SOURCES *.cpp)
    add_executable(${PROJECT_NAME} ${SOURCES})
    target_link_libraries(${PROJECT_NAME} Qt5::Core)

  • Your example and code worked fine for me with one exception. This line had to be changed to a capital C on core:

    find_package(Qt5 COMPONENTS Core REQUIRED)

    Here is the build output with make VERBOSE=1:

    -- The C compiler identification is GNU 6.2.1
    -- The CXX compiler identification is GNU 6.2.1
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/mike/tmp/x/build
    /usr/bin/cmake -H/home/mike/tmp/x -B/home/mike/tmp/x/build --check-build-system CMakeFiles/Makefile.cmake 0
    /usr/bin/cmake -E cmake_progress_start /home/mike/tmp/x/build/CMakeFiles /home/mike/tmp/x/build/CMakeFiles/progress.marks
    make -f CMakeFiles/Makefile2 all
    make[1]: Entering directory '/home/mike/tmp/x/build'
    make -f CMakeFiles/CMake_Test2_automoc.dir/build.make CMakeFiles/CMake_Test2_automoc.dir/depend
    make[2]: Entering directory '/home/mike/tmp/x/build'
    cd /home/mike/tmp/x/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/mike/tmp/x /home/mike/tmp/x /home/mike/tmp/x/build /home/mike/tmp/x/build /home/mike/tmp/x/build/CMakeFiles/CMake_Test2_automoc.dir/DependInfo.cmake --color=
    Dependee "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2_automoc.dir/DependInfo.cmake" is newer than depender "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2_automoc.dir/depend.internal".
    Dependee "/home/mike/tmp/x/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2_automoc.dir/depend.internal".
    Scanning dependencies of target CMake_Test2_automoc
    make[2]: Leaving directory '/home/mike/tmp/x/build'
    make -f CMakeFiles/CMake_Test2_automoc.dir/build.make CMakeFiles/CMake_Test2_automoc.dir/build
    make[2]: Entering directory '/home/mike/tmp/x/build'
    [ 20%] Automatic moc for target CMake_Test2
    /usr/bin/cmake -E cmake_autogen /home/mike/tmp/x/build/CMakeFiles/CMake_Test2_automoc.dir/ ""
    AUTOGEN: Checking /home/mike/tmp/x/main.cpp
    AUTOGEN: Checking /home/mike/tmp/x/test.cpp
    AUTOGEN: Checking /home/mike/tmp/x/test.h
    Generating moc source moc_test.cpp
    /usr/local/Qt/bin/moc -I/home/mike/tmp/x/build -I/home/mike/tmp/x -I/usr/local/Qt/include -I/usr/local/Qt/include/QtCore -I/usr/local/Qt/./mkspecs/linux-g++ -DQT_CORE_LIB -DQT_NO_DEBUG -o /home/mike/tmp/x/build/moc_test.cpp /home/mike/tmp/x/test.h
    Generating moc compilation CMake_Test2_automoc.cpp
    make[2]: Leaving directory '/home/mike/tmp/x/build'
    [ 20%] Built target CMake_Test2_automoc
    make -f CMakeFiles/CMake_Test2.dir/build.make CMakeFiles/CMake_Test2.dir/depend
    make[2]: Entering directory '/home/mike/tmp/x/build'
    cd /home/mike/tmp/x/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/mike/tmp/x /home/mike/tmp/x /home/mike/tmp/x/build /home/mike/tmp/x/build /home/mike/tmp/x/build/CMakeFiles/CMake_Test2.dir/DependInfo.cmake --color=
    Dependee "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2.dir/DependInfo.cmake" is newer than depender "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2.dir/depend.internal".
    Dependee "/home/mike/tmp/x/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/mike/tmp/x/build/CMakeFiles/CMake_Test2.dir/depend.internal".
    Scanning dependencies of target CMake_Test2
    make[2]: Leaving directory '/home/mike/tmp/x/build'
    make -f CMakeFiles/CMake_Test2.dir/build.make CMakeFiles/CMake_Test2.dir/build
    make[2]: Entering directory '/home/mike/tmp/x/build'
    [ 40%] Building CXX object CMakeFiles/CMake_Test2.dir/main.cpp.o
    /usr/bin/c++   -DQT_CORE_LIB -DQT_NO_DEBUG -I/home/mike/tmp/x/build -I/home/mike/tmp/x -isystem /usr/local/Qt/include -isystem /usr/local/Qt/include/QtCore -isystem /usr/local/Qt/./mkspecs/linux-g++  -fPIC -std=gnu++11 -o CMakeFiles/CMake_Test2.dir/main.cpp.o -c /home/mike/tmp/x/main.cpp
    [ 60%] Building CXX object CMakeFiles/CMake_Test2.dir/test.cpp.o
    /usr/bin/c++   -DQT_CORE_LIB -DQT_NO_DEBUG -I/home/mike/tmp/x/build -I/home/mike/tmp/x -isystem /usr/local/Qt/include -isystem /usr/local/Qt/include/QtCore -isystem /usr/local/Qt/./mkspecs/linux-g++  -fPIC -std=gnu++11 -o CMakeFiles/CMake_Test2.dir/test.cpp.o -c /home/mike/tmp/x/test.cpp
    [ 80%] Building CXX object CMakeFiles/CMake_Test2.dir/CMake_Test2_automoc.cpp.o
    /usr/bin/c++   -DQT_CORE_LIB -DQT_NO_DEBUG -I/home/mike/tmp/x/build -I/home/mike/tmp/x -isystem /usr/local/Qt/include -isystem /usr/local/Qt/include/QtCore -isystem /usr/local/Qt/./mkspecs/linux-g++  -fPIC -std=gnu++11 -o CMakeFiles/CMake_Test2.dir/CMake_Test2_automoc.cpp.o -c /home/mike/tmp/x/build/CMake_Test2_automoc.cpp
    [100%] Linking CXX executable CMake_Test2
    /usr/bin/cmake -E cmake_link_script CMakeFiles/CMake_Test2.dir/link.txt --verbose=1
    /usr/bin/c++      CMakeFiles/CMake_Test2.dir/main.cpp.o CMakeFiles/CMake_Test2.dir/test.cpp.o CMakeFiles/CMake_Test2.dir/CMake_Test2_automoc.cpp.o  -o CMake_Test2 -rdynamic /usr/local/Qt/lib/libQt5Core.so.5.7.0 -Wl,-rpath,/usr/local/Qt/lib
    make[2]: Leaving directory '/home/mike/tmp/x/build'
    [100%] Built target CMake_Test2
    make[1]: Leaving directory '/home/mike/tmp/x/build'
    /usr/bin/cmake -E cmake_progress_start /home/mike/tmp/x/build/CMakeFiles 0

    Again this used your test.h/cpp and main.cpp, as well as your posted CMakeFiles.txt. I used cmake 3.6.2 and Qt 5.7 for the test if that matters (it shouldn't).

  • Lifetime Qt Champion

    @ambershark Nice catch ! I missed that one.

    On a side note, aux_source_directory as used in the sample code goes against the CMake recommandation.

  • @SGaist Oh I would have missed it too if I didn't try to run his example. Case stuff is hard to see without a compiler issue. :)

  • Lifetime Qt Champion

    I also ran it but I didn't saw anything about the component itself but I may have very well missed it.

  • Tried both solutions: And both work! It seems that the capital c on core make the difference, now moc is triggered correctly. It work also for the "real project".

    Thanks guys!!

Log in to reply