Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

undefined reference to `QSerialPort::staticMetaObject'



  • I am trying to put the QSerialPort::BaudRate enum type as a property to in my class. The library for which the class is apart of will compile fine. I use google unit test + qt test to test the library. When I try to compile my unit test application it fails with the undefined reference to `QSerialPort::staticMetaObject'. I looked https://forum.qt.io/topic/105171/undefined-reference-to-name-staticmetaobject-when-registering-with-qmlregistertype on this thread but there doesnt seem to be a solution. Do I need to declare the QSerialPort as a metatype.

    high-level cmake

    cmake_minimum_required(VERSION 3.5.0)
    set(QTVersion 5.15.0 CACHE STRING "Version of QT")
    set(FRAMEWORK_BUILD_VERSION 0.1.0 CACHE STRING "Version of the Framework")
    set(COMMON_BUILD_VERSION 0.1.0 CACHE STRING "Version of the Common Library")
    set(INJECTORCLIENT_BUILD_VERSION 0.1.0 CACHE STRING "Version of the Client Library")
    set(SYSTEMSERVICE_BUILD_VERSION 0.1.0 CACHE STRING "Version of the Service Library")
    set(UNITTESTS_BUILD_VERSION 0.1.0 CACHE STRING "Version of the UnitTests Suite")
    
    project(RADIANT-SCP-Framework VERSION ${FRAMEWORK_BUILD_VERSION})
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_C_STANDARD 99)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    enable_testing()
    
    add_subdirectory(Common)
    add_subdirectory(InjectorClient)
    add_subdirectory(SystemService)
    option(INSTALL_GMOCK "Install Googletest's GMock?" OFF)
    option(INSTALL_GTEST "Install Googletest's GTest?" OFF)
    add_subdirectory(googletest)
    add_subdirectory(UnitTests)
    
    set(ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
    if(NOT "${CMAKE_CXX_COMPILER_TARGET}" STREQUAL "")
    set(ARCH ${CMAKE_CXX_COMPILER_TARGET})
    endif()
    SET(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
    SET(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${FRAMEWORK_BUILD_VERSION}-Qt${QTVersion}-${ARCH}-${CMAKE_BUILD_TYPE}")
    SET(CPACK_SET_DESTDIR ON)
    SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "KK")
    set(CPACK_VERBATIM_VARIABLES YES)
    INCLUDE(CPack)
    

    Libraries CMAKE File

    project(RADIANT-SCP-Common VERSION ${COMMON_BUILD_VERSION})
    
    find_package(Qt5 COMPONENTS Core SerialPort REQUIRED)
    
    add_library(RADIANT-SCP-Common SHARED src/helloworld.h
                                          src/helloworld.cpp
                                          src/common_global.h
                                          src/utils/logging/logger.cpp
                                          src/utils/logging/logger.h
                                          src/utils/logging/loggerconfig.h
                                          src/utils/filesystem/filesystemutils.cpp
                                          src/utils/filesystem/filesystemutils.h
                                          src/datamodels/basedatamodel.h
                                          src/datamodels/basedatamodel.cpp
                                          src/datamodels/digestdefs.h
                                          src/datamodels/ICPAPI/ICPComm/Serial/serialcommmodel.h
                                          src/datamodels/ICPAPI/ICPComm/Serial/serialcommmodel.cpp)
    
    target_link_libraries(RADIANT-SCP-Common PRIVATE Qt5::Core)
    
    target_include_directories(RADIANT-SCP-Common PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
    
    target_compile_definitions(RADIANT-SCP-Common PRIVATE COMMON_LIBRARY)
    
    INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/include
            DESTINATION /usr/local/include/RADIANT/SCP/Common
            FILES_MATCHING PATTERN "*.h")
    INSTALL(TARGETS RADIANT-SCP-Common
                LIBRARY DESTINATION /usr/lib)
    

    class header file

    #ifndef SERIALCOMMMODEL_H
    #define SERIALCOMMMODEL_H
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    #include "../../../basedatamodel.h"
    #include "../../../../common_global.h"
    
    namespace RADIANT::SCP::Common {
    
    class COMMON_EXPORT SerialCommModel: public BaseDataModel
    {
        Q_OBJECT
        Q_PROPERTY(QSerialPort::BaudRate baudRate READ baudRate WRITE setBaudRate NOTIFY DataModelChanged)
    

    cmake unit test application

    project(RADIANT-SCP-UnitTests VERSION ${UNITTESTS_BUILD_VERSION})
    
    find_package(Qt5 COMPONENTS Core SerialPort REQUIRED)
    find_package(Qt5Test REQUIRED)
    
    set(Headers
        Common/helloworld_test.h
        Common/utils/logging/logger_test.h
        Common/utils/filesystem/filesystemutils_test.h
        InjectorClient/helloworldinjectorclient_test.h
        SystemService/helloworldsystemservice_test.h
       )
    set(Sources
        main.cpp)
    add_executable(RADIANT-SCP-UnitTests ${Sources} ${Headers})
    
    
    target_link_libraries(RADIANT-SCP-UnitTests PRIVATE Qt5::Core
                                                        Qt5::Test)
    
    target_link_libraries(RADIANT-SCP-UnitTests PUBLIC
                          gtest_main
    		      RADIANT-SCP-Common
    		      RADIANT-SCP-InjectorClient
                          RADIANT-SCP-SystemService)
    
    set_target_properties(RADIANT-SCP-UnitTests PROPERTIES LINKER_LANGUAGE CXX)
    
    add_test(
    	NAME RADIANT-SCP-UnitTests
            COMMAND RADIANT-SCP-UnitTests
    	)
    
    

    Build output

    CMake Warning (dev) in googletest/googlemock/CMakeLists.txt:
      AUTOGEN: No valid Qt version found for target gmock.  AUTOMOC, AUTOUIC and
      AUTORCC disabled.  Consider adding:
    
        find_package(Qt<QTVERSION> COMPONENTS Widgets)
    
      to your CMakeLists.txt file.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    CMake Warning (dev) in googletest/googlemock/CMakeLists.txt:
      AUTOGEN: No valid Qt version found for target gmock_main.  AUTOMOC, AUTOUIC
      and AUTORCC disabled.  Consider adding:
    
        find_package(Qt<QTVERSION> COMPONENTS Widgets)
    
      to your CMakeLists.txt file.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    CMake Warning (dev) in googletest/googletest/CMakeLists.txt:
      AUTOGEN: No valid Qt version found for target gtest_main.  AUTOMOC, AUTOUIC
      and AUTORCC disabled.  Consider adding:
    
        find_package(Qt<QTVERSION> COMPONENTS Widgets)
    
      to your CMakeLists.txt file.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    CMake Warning (dev) in googletest/googletest/CMakeLists.txt:
      AUTOGEN: No valid Qt version found for target gtest.  AUTOMOC, AUTOUIC and
      AUTORCC disabled.  Consider adding:
    
        find_package(Qt<QTVERSION> COMPONENTS Widgets)
    
      to your CMakeLists.txt file.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    -- Generating done
    -- Build files have been written to: /home/developer/src/RADIANT/SCP/radiant.scp.framework/builds/0.1.0/Qt5.15.0/gcc_64/Debug
    [13/13] Linking CXX executable UnitTests/RADIANT-SCP-UnitTests
    FAILED: UnitTests/RADIANT-SCP-UnitTests 
    : && /usr/bin/c++  -g   UnitTests/CMakeFiles/RADIANT-SCP-UnitTests.dir/RADIANT-SCP-UnitTests_autogen/mocs_compilation.cpp.o UnitTests/CMakeFiles/RADIANT-SCP-UnitTests.dir/main.cpp.o  -o UnitTests/RADIANT-SCP-UnitTests  -Wl,-rpath,/home/developer/Qt/5.15.0/gcc_64/lib:/home/developer/src/RADIANT/SCP/radiant.scp.framework/builds/0.1.0/Qt5.15.0/gcc_64/Debug/InjectorClient:/home/developer/src/RADIANT/SCP/radiant.scp.framework/builds/0.1.0/Qt5.15.0/gcc_64/Debug/SystemService:/home/developer/src/RADIANT/SCP/radiant.scp.framework/builds/0.1.0/Qt5.15.0/gcc_64/Debug/Common  /home/developer/Qt/5.15.0/gcc_64/lib/libQt5Test.so.5.15.0  lib/libgtest_maind.a  InjectorClient/libRADIANT-SCP-InjectorClient.so  SystemService/libRADIANT-SCP-SystemService.so  /home/developer/Qt/5.15.0/gcc_64/lib/libQt5Core.so.5.15.0  lib/libgtestd.a  -lpthread  Common/libRADIANT-SCP-Common.so  -Wl,-rpath-link,/home/developer/Qt/5.15.0/gcc_64/lib && :
    /usr/bin/ld: Common/libRADIANT-SCP-Common.so: undefined reference to `QSerialPort::staticMetaObject'
    

    any help would be greatly appericated



  • @pjorourke05 use target_link_libraries(RADIANT-SCP-Common PRIVATE Qt5::Core Qt5::SerialPort)


  • Lifetime Qt Champion

    You don't link against QtSerialport as it seems.



  • @pjorourke05 use target_link_libraries(RADIANT-SCP-Common PRIVATE Qt5::Core Qt5::SerialPort)



  • that did the trick thanks!


Log in to reply