Problem when using cppcheck with Qt 6.4.2 on Ubuntu 22 "the header file doesn't include <QObject>"

327 Views Asked by At

Before introducing the problem I will give you some device information:

  • Operating system: Ubuntu 22.04.2 LTS
  • Qt v6.4.2
  • CMake v3.22.1
  • cppcheck v2.7

The problem is the following: I have a GUI desktop app which uses Qt and is compiled with CMake (so I am not using qt-creator or qmake).

My CMakeLists.txt file:

# Project settings
cmake_minimum_required( VERSION 3.15 )

project( key-manager-build-src
    VERSION 1.0
    DESCRIPTION "Build system for key manager source code."
    LANGUAGES CXX
)

# Error if building out of a build directory
file( TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH )
if( EXISTS "${LOC_PATH}" )
    message( FATAL_ERROR "You cannot build in a source directory (or any directory with "
                         "CMakeLists.txt file). Please make a build subdirectory. Feel free to "
                         "remove CMakeCache.txt and CMakeFiles." )
endif()

# Set compiler options
set( CMAKE_CXX_STANDARD 20 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )

# Specific Qt settings
set( CMAKE_AUTOMOC ON )
set( CMAKE_AUTORCC ON )
set( CMAKE_AUTOUIC ON )
set( CMAKE_INCLUDE_CURRENT_DIR ON ) 

# Include directories
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/../include )

# File variables
file( GLOB_RECURSE INC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.hpp )
file( GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../src/*.cpp )

# Creating the main executable
set( APP "key-manager" )
set( LIB "key-manager-static" )
add_executable( ${APP} ${SRC_FILES} ${INC_FILES} )

# Adding specific compiler flags
if( CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
    set( COMPILE_FLAGS "/Wall /Yd" )
else()
    if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
        set( COMPILE_FLAGS "-Wall -Wextra -pedantic -Wno-reorder -g" )
    else()
        set( COMPILE_FLAGS "-Wall -Wextra -pedantic -Wno-reorder" )
    endif()
endif()
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}" )

# Adding cppcheck properties ---> NOTE THIS PART
if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
    set( cppcheck cppcheck "--enable=warning" "--inconclusive" "--force" "--inline-suppr" )
    set_target_properties( ${APP} PROPERTIES CXX_CPPCHECK ${cppcheck})
endif()

# Link to Qt6
find_package( Qt6 CONFIG REQUIRED COMPONENTS Core Widgets StateMachine )
target_link_libraries( ${APP} PRIVATE Qt::Core Qt::Widgets Qt::StateMachine )

# Install
install ( TARGETS ${APP} RUNTIME )

as you can see I am using cppcheck within CMake compilation to highlight possible problems.

But the error when compiling with cmake --build build -DCMAKE_BUILD_TYPE=Debug I got is the following:

[  4%] Automatic MOC and UIC for target key-manager
[  4%] Built target key-manager_autogen
[  9%] Building CXX object src/CMakeFiles/key-manager.dir/key-manager_autogen/mocs_compilation.cpp.o
Checking /home/gianluca/key-manager/build/src/key-manager_autogen/mocs_compilation.cpp ...
/home/gianluca/key-manager/build/src/key-manager_autogen/UNUAP36XLB/moc_add_password_state.cpp:14:0: error: #error "The header file 'add_password_state.hpp' doesn't include <QObject>." [preprocessorErrorDirective]
#error "The header file 'add_password_state.hpp' doesn't include <QObject>."
^
[ 13%] Building CXX object src/CMakeFiles/key-manager.dir/app.cpp.o
Checking /home/gianluca/key-manager/src/app.cpp ...
Checking /home/gianluca/key-manager/src/app.cpp: DEBUG_KEY_MANAGER=1;QT_CORE_LIB=1;QT_GUI_LIB=1;QT_STATEMACHINE_LIB=1;QT_WIDGETS_LIB=1...
...
...
"""Everything else is ok"""

The fact is that add_password_state.cpp has the following headers

...
// Qt
#include <QState>
#include <QLabel>
#include <QSize>
#include <QJsonDocument>
#include <QJsonValue>
#include <QFile>
#include <QTextStream>
#include <QTimer>
#include <QIODevice>
#include <QLineEdit>
#include <QObject>
#include <QPushButton>
#include <QStringConverter>
#include <QVariant>
#include <QWidget>

// STD
#include <filesystem>
#include <string>
#include <fstream>
...

and add_password_state.hpp has these:

// Qt
#include <QState>
#include <QSharedPointer>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QObject>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QByteArray>
#include <QString>
#include <QObject>

// STD
#include <sstream>

So the QObject header is included! The fact is that the compilation finish successfully and the executable is produced, but this "error" appears everytime I compile and seems to be some sort of false-positive. What do you think about it? Thanks in advice.

0

There are 0 best solutions below