Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a #define for checking if the compiler is Qt?
Forum Updated to NodeBB v4.3 + New Features

Is there a #define for checking if the compiler is Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 11 Posters 9.7k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hskoglundH hskoglund

    Try

    #if defined(QT_VERSION)
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #25

    @hskoglund
    Where does QT_VERSION get defined, please? Isn't it via #include <QtGlobal>, so it tells you whether or not you have already included that file? How do you decide whether to include that or not? That's the bit I don't get....

    @kitfox
    OK, to be fair/clear, surrounding some Qt-only code in #if defined(QT_VERSION) looks like a good model for most of your code. It's a clean symbol. So that's good.

    You still have to "get going" somewhere, to decide whether to define that or include QtGlobal.h or not in the first place.

    K J.HilkJ 2 Replies Last reply
    0
    • JonBJ JonB

      @hskoglund
      Where does QT_VERSION get defined, please? Isn't it via #include <QtGlobal>, so it tells you whether or not you have already included that file? How do you decide whether to include that or not? That's the bit I don't get....

      @kitfox
      OK, to be fair/clear, surrounding some Qt-only code in #if defined(QT_VERSION) looks like a good model for most of your code. It's a clean symbol. So that's good.

      You still have to "get going" somewhere, to decide whether to define that or include QtGlobal.h or not in the first place.

      K Offline
      K Offline
      kitfox
      wrote on last edited by
      #26

      @JonB Yeah, I realized that after I made the last post. For now I'll add an extra define to my build file.

      JonBJ 1 Reply Last reply
      1
      • K kitfox

        @JonB Yeah, I realized that after I made the last post. For now I'll add an extra define to my build file.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #27

        @kitfox
        :) :) Which is what I've been trying to get at all along :)

        1 Reply Last reply
        0
        • JonBJ JonB

          @hskoglund
          Where does QT_VERSION get defined, please? Isn't it via #include <QtGlobal>, so it tells you whether or not you have already included that file? How do you decide whether to include that or not? That's the bit I don't get....

          @kitfox
          OK, to be fair/clear, surrounding some Qt-only code in #if defined(QT_VERSION) looks like a good model for most of your code. It's a clean symbol. So that's good.

          You still have to "get going" somewhere, to decide whether to define that or include QtGlobal.h or not in the first place.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #28

          @JonB said in Is there a #define for checking if the compiler is Qt?:

          Where does QT_VERSION get defined, please? Isn't it via #include <QtGlobal>, so it tells you whether or not you have already included that file? How do you decide whether to include that or not? That's the bit I don't get....

          nope, its part of qmake, and the define is available inside the *.pro file therefore influencing the generated make file.

          Its generally used to check if some features are available in the Qt version you're using or not, to therefore include specific files(or not)


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          JonBJ 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @JonB said in Is there a #define for checking if the compiler is Qt?:

            Where does QT_VERSION get defined, please? Isn't it via #include <QtGlobal>, so it tells you whether or not you have already included that file? How do you decide whether to include that or not? That's the bit I don't get....

            nope, its part of qmake, and the define is available inside the *.pro file therefore influencing the generated make file.

            Its generally used to check if some features are available in the Qt version you're using or not, to therefore include specific files(or not)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #29

            @J-Hilk
            Ah, now when I Googled yesterday I found a post (stackoverflow??) which said that QT_VERSION was defined in QtGlobal. Admittedly I think the post was from years ago, but I often find Qt is behaving the same as at the turn of the millennium :) Ah, yes, see https://stackoverflow.com/a/24903583/489865. And that was as recent as 2015. If you think that is inaccurate, you might wish to post there :)

            What you say is actually a bit strange: the header files etc. belong to a particular Qt version, hence it would make sense if they define the Qt version for which they work. Defining your own value for QT_VERSION isn't going to help much when they were written for a different version....

            jsulmJ 1 Reply Last reply
            0
            • JonBJ JonB

              @J-Hilk
              Ah, now when I Googled yesterday I found a post (stackoverflow??) which said that QT_VERSION was defined in QtGlobal. Admittedly I think the post was from years ago, but I often find Qt is behaving the same as at the turn of the millennium :) Ah, yes, see https://stackoverflow.com/a/24903583/489865. And that was as recent as 2015. If you think that is inaccurate, you might wish to post there :)

              What you say is actually a bit strange: the header files etc. belong to a particular Qt version, hence it would make sense if they define the Qt version for which they work. Defining your own value for QT_VERSION isn't going to help much when they were written for a different version....

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #30

              @JonB qmake you're using defines which Qt version is used :-)
              So, it is perfectly valid if qmake defines QT_VERSION.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @JonB qmake you're using defines which Qt version is used :-)
                So, it is perfectly valid if qmake defines QT_VERSION.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #31

                @jsulm
                Since you posted, I have edited mine to link to the reference I used for this statement. Are the answers there wrong? Do you wish to correct them? :)

                Also, I have just looked at https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html. There, bold as brass, unconditional (e.g. it is not inside #ifndef QT_VERSION):

                #define QT_VERSION      QT_VERSION_CHECK(QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH)
                

                Does that not define the QT_VERSION, yes or no?

                J.HilkJ kshegunovK 2 Replies Last reply
                0
                • JonBJ JonB

                  @jsulm
                  Since you posted, I have edited mine to link to the reference I used for this statement. Are the answers there wrong? Do you wish to correct them? :)

                  Also, I have just looked at https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html. There, bold as brass, unconditional (e.g. it is not inside #ifndef QT_VERSION):

                  #define QT_VERSION      QT_VERSION_CHECK(QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH)
                  

                  Does that not define the QT_VERSION, yes or no?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #32

                  @JonB you're also correct,

                  the first thing one does, when building the Qt libs, is building qmake, and IIRC that uses the QtGlobal of the source tree it belongs to...


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • JonBJ JonB

                    @jsulm
                    Since you posted, I have edited mine to link to the reference I used for this statement. Are the answers there wrong? Do you wish to correct them? :)

                    Also, I have just looked at https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html. There, bold as brass, unconditional (e.g. it is not inside #ifndef QT_VERSION):

                    #define QT_VERSION      QT_VERSION_CHECK(QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH)
                    

                    Does that not define the QT_VERSION, yes or no?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #33

                    @JonB said in Is there a #define for checking if the compiler is Qt?:

                    Does that not define the QT_VERSION, yes or no?

                    Correct. The qmake bootstrap has little to do with your code. I can't say for sure that qmake doesn't define the macro for your project, but it shouldn't (provided you've used CONFIG -= qt).

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • K kitfox

                      That's right. I'm having trouble thinking of a way to rewrite ffmpeg-cpp_global.h so that it would work either inside of outside of a Qt environment. Currently FFMPEGCPP_LIBRARY is defined in the .pro file and acts as a switch - if it's included, then FFMPEGCPP_EXPORT transforms into instructions to export the definition to a dll. However, if it's not included (which it presumably would not be to anyone who wanted to use the library) it turns into a declaration to import the definition - instructions which depend on the Qt API to work. So what am I supposed to use if someone it trying to compile this with another compiler? I'd like a way to essentially nullify everything in this header file if I can tell that the person trying to compile it is not using Qt.

                      As far as I can tell, I'm going to have to have everyone using Qt who wants to use this library add some define in their .pro file so that the imports are generated. Ie, something like:

                      
                      #ifdef IM_USING_QT
                      
                      #include <QtCore/qglobal.h>
                      
                      #if defined(FFMPEGCPP_LIBRARY)
                      #  define FFMPEGCPP_EXPORT Q_DECL_EXPORT
                      #else
                      #  define FFMPEGCPP_EXPORT Q_DECL_IMPORT
                      #endif
                      
                      #else 
                      
                      #define FFMPEGCPP_EXPORT
                      
                      #endif
                      
                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #34

                      @kitfox said in Is there a #define for checking if the compiler is Qt?:

                      That's right. I'm having trouble thinking of a way to rewrite ffmpeg-cpp_global.h so that it would work either inside of outside of a Qt environment. Currently FFMPEGCPP_LIBRARY is defined in the .pro file and acts as a switch - if it's included, then FFMPEGCPP_EXPORT transforms into instructions to export the definition to a dll. However, if it's not included (which it presumably would not be to anyone who wanted to use the library) it turns into a declaration to import the definition - instructions which depend on the Qt API to work.

                      
                      #ifdef IM_USING_QT
                      
                      #include <QtCore/qglobal.h>
                      
                      #if defined(FFMPEGCPP_LIBRARY)
                      #  define FFMPEGCPP_EXPORT Q_DECL_EXPORT
                      #else
                      #  define FFMPEGCPP_EXPORT Q_DECL_IMPORT
                      #endif
                      
                      #else 
                      
                      #define FFMPEGCPP_EXPORT
                      
                      #endif
                      

                      Sorry to take a step back here but I think we got sidetracked.

                      In the code posted the only "Qt" code is Q_DECL_EXPORT and Q_DECL_IMPORT. They can easily be replaced with native code

                      #  if (defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
                      #    define NQ_DECL_EXPORT     __declspec(dllexport)
                      #    define NQ_DECL_IMPORT     __declspec(dllimport)
                      #  else
                      #    define NQ_DECL_EXPORT     __attribute__((visibility("default")))
                      #    define NQ_DECL_IMPORT     __attribute__((visibility("default")))
                      #  endif
                      
                      #if defined(FFMPEGCPP_LIBRARY)
                      #  define FFMPEGCPP_EXPORT NQ_DECL_EXPORT
                      #else
                      #  define FFMPEGCPP_EXPORT NQ_DECL_IMPORT
                      #endif
                      

                      Now it should build and link with any build system. If you are building the library itself the build system (qmake, qbs, CMake, Visual Studio, etc.) will need to add a compiler definition for FFMPEGCPP_LIBRARY while user of the library will not. This is 100% standard in C/C++ libraries

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      8
                      • S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #35

                        @VRonin is correct that other ways are preferrable to define DLL import/export.

                        I also assumed that QT_VERSION had to be defined by qmake. However, I had a quick look at the generated Makefile. And what is defined are things like QT_CORE_LIB, QT_WIDGETS_LIB, etc. My guess is that QT_WIDGETS_LIB, e.g., will appear if you put QT += widgets into your .pro file. And I would expect that QT_CORE_LIB is always defined when you compile with Qt.

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved