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

Using Qt: Defined C++ symbol



  • I'm developing a C++ shared library using Qt and CMake.
    I'm trying to write a single source-code that be possible to compile in machines with or without installed Qt environment.

    To do that, I'm using a custom #ifdef USING_QT symbol to enable/disable blocks of code depending of the compile system.
    Example: with that, I can disable the Q_OBJECT macro and the signals and slots methods.

    #ifdef USING_QT
        // Code that used Qt features
    #else
        // The same code when Qt environment is not available
    #endif
    

    Now, I would like to replace this custom C++ symbol to a official Qt symbol that always will be present if the developer try to compile this project using Qt.

    I was using the #ifdef QT_VERSION in the past (Qt 5.11 and Qt Creator IDE 4.10.0), but in this new project, when I try to use this Qt macro, the Qt Creator IDE mark the internal code as disabled, like if this symbol was not defined.

    Today, I'm using Qt 5.15.0 and Qt Creator 4.12.4 on Ubuntu 20.04 x64.

    #ifdef QT_VERSION
        x = 10; // QT CREATOR PUTS GREY COLOR (Disabled)
    #endif
    

    Is there a better official Qt symbol to choice in this case?

    May be, there was any changes in Qt Libraries or Qt Creator IDE that changed the text editor color highlight to disable blocks of code inside the #ifdef QT_VERSION macro? I don't know...

    Obs.: I searched some macros in QtGlobal https://doc.qt.io/qt-5/qtglobal.html but I couldn't found a good option.

    Thank you,



  • In Cmake, you could do something like

    find_package(Qt5 COMPONENTS Core Gui Widgets)
    if (Qt5_FOUND)
        target_compile_definitions(MyCoolLibrary PUBLIC MYCOOLLIBRARY_USE_QT=1)
    endif()
    

    If you don't include any Qt headers, there won't be anything like QT_VERSION defined. The definitions for that stuff is in those Qt headers, so you can't use it to decide whether or not to include them. You need to use your own flag in the configuration to make the decision.


  • Qt Champions 2017

    What I'd do is test this during the make generation step (qmake code, you need to check yourself how to do it with cmake as I'm not familiar with what's available there):

    CONFIG(qt): DEFINES += USING_QT  # We are building with/against Qt
    


  • In Cmake, you could do something like

    find_package(Qt5 COMPONENTS Core Gui Widgets)
    if (Qt5_FOUND)
        target_compile_definitions(MyCoolLibrary PUBLIC MYCOOLLIBRARY_USE_QT=1)
    endif()
    

    If you don't include any Qt headers, there won't be anything like QT_VERSION defined. The definitions for that stuff is in those Qt headers, so you can't use it to decide whether or not to include them. You need to use your own flag in the configuration to make the decision.



  • @wrosecrans thank you!



  • @kshegunov said in Using Qt: Defined C++ symbol:

    how to do it with cmake

    add_definitions(-DUSING_QT)
    

  • Qt Champions 2017

    @fem_dev said in Using Qt: Defined C++ symbol:

    add_definitions(-DUSING_QT)
    

    The point was that the distinction could, and I'd argue it should, be made automatically, which as far as I can tell is @wrosecrans' solution.


Log in to reply