CMake not generating Qt moc rules

717 Views Asked by At

This is a debugging question. I am trying to integrate Qt moc'ing into a CMake managed project using the AUTOMOC feature. I have not managed to generate a project which includes moc rules, though I believe I followed the guidelines.

I've checked with the following command that the expected files are indeed listed :

get_target_property(MY_PROJECT_SOURCES CMAKETEST SOURCES)
message("${MY_PROJECT_SOURCES}")

I did notice that ${AUTOMOC_MACRO_NAMES} was initially empty and not initialized to CMAKE_AUTOMOC_MACRO_NAME as the documentation claims, which may be a hint as to the cause of the problem. I tried to add an explicit set(AUTOMOC_MACRO_NAMES "${CMAKE_AUTOMOC_MACRO_NAMES}") to try to work around that.

I've written a MCVE which reproduces the problem on my system. Here is the CMakeList file :

# CMake version expected
cmake_minimum_required(VERSION 3.18)

# Project title
project(CMAKETEST)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# specify source files
set(DIR_SRC "${CMAKETEST_SOURCE_DIR}/src")
file(GLOB_RECURSE SRC_LIST ${DIR_SRC}/*.cpp ${DIR_SRC}/*.h ${DIR_SRC}/*.hpp)

add_executable(CMAKETEST ${SRC_LIST})


# include Qt

# enable Qt support
set_target_properties(CMAKETEST PROPERTIES AUTOMOC TRUE)
set_target_properties(CMAKETEST PROPERTIES AUTOUIC TRUE)
set_target_properties(CMAKETEST PROPERTIES AUTORCC TRUE)

# Find Qt libs
set(QT_DIR $ENV{QT_HOME_DIR})
set(CMAKE_PREFIX_PATH "${QT_DIR}")
find_package(Qt5 COMPONENTS Core REQUIRED)
target_link_libraries(CMAKETEST Qt5::Core)
set(AUTOMOC_MACRO_NAMES "${CMAKE_AUTOMOC_MACRO_NAMES}")

The project file tree looks like :

project_root
│   CMakeLists.txt
│
├───build
└───src
        main.cpp
        my_qt_obj.cpp
        my_qt_obj.h

my_qt_obj.h :

#pragma once
#include <QObject>

class my_qt_obj : public QObject
{
    Q_OBJECT
public slots:
    void my_slot();
signals:
    void my_sig();
};

my_qt_obj.cpp :

#include "my_qt_obj.h"
void my_qt_obj::my_slot() {}

main.cpp :

#include "my_qt_obj.h"
int main() {
    my_qt_obj foo;
    QObject::connect(&foo, &my_qt_obj::my_sig, &foo, &my_qt_obj::my_slot);
}

Predictably, since no moc rules are generated, I get a linking error due to unresolved external symbols. But there are no errors reported by CMake.

The command I use to build is : cmake -S . -B build -A x64 from the project root directory. I am using CMake 3.18.3 with Qt 5.15.1 compiled from source. The commands used to compile Qt are :

"%QT_SRC%\configure" -prefix "%QT_OUT%" -platform win32-msvc -opengl desktop -make libs -nomake examples -nomake tests -skip qtwebengine -opensource
nmake    
nmake install

My attempts have all been targeting Visual Studio 2017.

0

There are 0 best solutions below