CMake: how to instruct automoc to ignore certain files?



  • Hi fellow devs

    I'm using CMake (v. 3.4.3) for my C++ project (min. cmake version 3.0), and Qt 5.6.0. Some of my libraries are UI related, some not. For the UI-related ones, I'm getting errors in my files which do not define the Q_OBJECT macro (it must be like that, I don't need all files with Q_OBJECT). This is the error:

    Error: Class declaration lacks Q_OBJECT macro
    

    Does anyone have an idea of how to tell cmake not to run moc on these files without Q_OBJECT? I'm running a CentOS 7.2 machine, 64bit. Thanks for your time.


  • Lifetime Qt Champion

    Hi,

    Are you talking about designer based widgets ?



  • Hi

    Thanks for answering. The UI layout I make in designer, and the functionality I implement in C++, normal stuff. But not all of my C++ classes need to be backed up by a widget.


  • Lifetime Qt Champion

    So if I understand correctly, you are removing the Q_OBJECT macro from your UI based widgets ?



  • No, it's the other way around. For UI-based widgets, I have the Q_OBJECT macro, for the other classes not.


  • Lifetime Qt Champion

    Can you show your CMakeLists.txt and a sample class that triggers this problem ?



  • Sure, here they are. My apologies for not posting this earlier to save us both time.

    CMakeLists.txt (ignore the comments, they are from an old project)

    set (LIBNAME kissqt)
    
    file (GLOB HEADERS
    	  "*.h"
    	  "*.hpp")
    
    file (GLOB SOURCES "*.cpp")
    file (GLOB UI_FILES "*.ui")
    
    # Create named folders for the sources within the .vcproj
    # Empty name lists them directly under the .vcproj
    source_group("headers" FILES ${HEADERS})
    source_group("src" FILES ${SOURCES})
    source_group("ui" FILES ${UI_FILES})
    
    # Properties->C/C++->General->Additional Include Directories
    include_directories (.)
    include_directories (..)
    include_directories (/home/ksoft/Qt5.6.0/5.6/gcc_64/include/)
    
    set(CMAKE_AUTOMOC_RELAXED_MODE TRUE)
    
    find_package(Qt5Widgets)
    QT5_WRAP_CPP(tst_hdr_moc ${HEADERS})
    QT5_WRAP_UI(tst_form_hdr ${UI_FILES})
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    # Set Properties->General->Configuration Type to Dynamic Library(.dll)
    # Creates math.dll with the listed sources collected in the variables
    # Also adds sources to the Solution Explorer
    add_library(${LIBNAME} SHARED ${tst_hdr_moc} ${tst_form_hdr})
    
    set_target_properties(${LIBNAME} PROPERTIES 
    	AUTOMOC ON
    	AUTOUIC ON
    	DEFINE_SYMBOL "KISSQT_LIB"
    	OUTPUT_NAME ${LIBNAME}${LIB_END}
    	OUTPUT_NAME_DEBUG ${LIBNAME}${LIB_END_DEBUG}
    	OUTPUT_NAME_RELEASE ${LIBNAME}${LIB_END_RELEASE})
    
    # Set the library dependencies
    qt5_use_modules(${LIBNAME} Core Widgets Gui OpenGL Designer)
    
    # Creates a folder "libraries" and adds target project (math.vcproj) under it
    set_property(TARGET ${LIBNAME} PROPERTY FOLDER "lib")
    
    # Adds logic to INSTALL.vcproj to copy math.dll to the destination directory
    install (TARGETS ${LIBNAME}
    		 RUNTIME DESTINATION ${PROJECT_BINARY_DIR}/bin
    		 LIBRARY DESTINATION ${PROJECT_BINARY_DIR}/bin)
    
    

    and here the class

    #ifndef KQTDYNAMICINTERFACE_H
    #define KQTDYNAMICINTERFACE_H
    
    /*
    several forward declarations
    class CalcModule;
    etc...
    */
    
    class KQtDynamicInterface
    {
    public:
    	KQtDynamicInterface(CalcModule &module, QWidget *parent = 0);
    	virtual ~KQtDynamicInterface();
    	
    	/*
    	other methods
    		...
    	*/
    	
    protected:
    	/*
    	other methods
    		...
    	*/
    };
    
    #endif
    

    The error I get is this.

    kQtDynamicInterface.h:563: Error: Class declaration lacks Q_OBJECT macro.
    make[2]: *** [moc_kQtDynamicInterface.cpp] Error 1
    make[1]: *** [CMakeFiles/all] Error 2
    make: *** [all] Error 2
    07:13:42: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project 03-2017Linux (kit: Desktop)
    When executing step "Make"
    

  • Lifetime Qt Champion

    It's your use of QT5_WRAP_CPP which expect QObject based classes that triggers that error. Since you are using automoc, don't use QT5_WRAP_CPP.

    There's also autouic IIRC.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.