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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I'm modifying a third party library so it can be compiled in the Qt environment. I'd like to modify the following file so that FFMPEGCPP_EXPORT is empty if it is being compiled by something other than Qt. Is there a way to do this?

    #ifndef FFMPEGCPP_GLOBAL_H
    #define FFMPEGCPP_GLOBAL_H
    
    #include <QtCore/qglobal.h>
    
    #if defined(FFMPEGCPP_LIBRARY)
    #  define FFMPEGCPP_EXPORT Q_DECL_EXPORT
    #else
    #  define FFMPEGCPP_EXPORT Q_DECL_IMPORT
    #endif
    
    #endif // FFMPEGCPP_GLOBAL_H
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Qt is no compiler so what you are requesting is a bit strange.

      IIRC ffmpeg is a C library, so you don't need that but the proper include guards using extern "C" {} when you want to use it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        Okay, but Qt is an API and I need this file to compile even if it is compiled outside of the Qt environment. I'd like some sort of define so that I write something like:

        #ifdef QT_IS_AVAILABLE
        //Qt API specific stuff here
        #else
        //This is not Qt
        #endif
        
        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          But why are you modifying ffmpeg in the first place ?
          Why not have a custom class or just an include in your project that wraps whatever you need from that library ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • K Offline
            K Offline
            kitfox
            wrote on last edited by
            #5

            I'm not modifying ffmpeg. I'm modifying ffmpeg-cpp, a library which wraps ffmpeg and makes it easier to use.

            B 1 Reply Last reply
            0
            • K kitfox

              Okay, but Qt is an API and I need this file to compile even if it is compiled outside of the Qt environment. I'd like some sort of define so that I write something like:

              #ifdef QT_IS_AVAILABLE
              //Qt API specific stuff here
              #else
              //This is not Qt
              #endif
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @kitfox
              If you think about it, you will realize that you will have to define or not define your QT_IS_AVAILABLE from outside of Qt. So it would be flag you choose to define or not in, say, a Makefile, a -D-type argument you do or do not pass to the compiler line.

              Also, in your case you have already unconditionally included

              #include <QtCore/qglobal.h>
              

              so Qt will have to "be available" to get past that....

              1 Reply Last reply
              1
              • K Offline
                K Offline
                kitfox
                wrote on last edited by
                #7

                So there's no defines automatically set when using Qt to indicate that? I have to put a define in my .pro file?

                JonBJ 1 Reply Last reply
                0
                • K kitfox

                  So there's no defines automatically set when using Qt to indicate that? I have to put a define in my .pro file?

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

                  @kitfox
                  Sorry, I still don't think you're getting it. By the time you have a .pro file to put anything in, you're already compiling for Qt, aren't you?

                  SGaistS 1 Reply Last reply
                  1
                  • K Offline
                    K Offline
                    kitfox
                    wrote on last edited by
                    #9

                    I'm trying to format the code so that it can build either by clicking build within Qt Creator or by whatever compiler the original author was using.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @kitfox
                      Sorry, I still don't think you're getting it. By the time you have a .pro file to put anything in, you're already compiling for Qt, aren't you?

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      @kitfox
                      Sorry, I still don't think you're getting it. By the time you have a .pro file to put anything in, you're already compiling for Qt, aren't you?

                      No, you can build a Qt-less project.

                      @kitfox yes that's a define you have to enable yourself like shown in the Shared Library chapter of Qt's documentation.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      JonBJ 1 Reply Last reply
                      1
                      • SGaistS SGaist

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

                        @kitfox
                        Sorry, I still don't think you're getting it. By the time you have a .pro file to put anything in, you're already compiling for Qt, aren't you?

                        No, you can build a Qt-less project.

                        @kitfox yes that's a define you have to enable yourself like shown in the Shared Library chapter of Qt's documentation.

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

                        @SGaist
                        Why would you start with a .pro file if you don't have Qt anywhere? What are you going to process it with?

                        SGaistS 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @SGaist
                          Why would you start with a .pro file if you don't have Qt anywhere? What are you going to process it with?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @JonB do you mean if you don't use Qt in your project ? Maybe because you are used to use qmake to manage your projects and are satisfied with it.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          JonBJ 1 Reply Last reply
                          1
                          • SGaistS SGaist

                            @JonB do you mean if you don't use Qt in your project ? Maybe because you are used to use qmake to manage your projects and are satisfied with it.

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

                            @SGaist
                            I envisage the OP is asking: he gives all his code plus Makefiles to someone else, who may not possess Qt at all, no qmake etc. And they can compile the code some other way with nothing Qt anywhere.

                            Then at one point he said

                            either by clicking build within Qt Creator or by whatever compiler the original author was using.

                            So maybe he'd like to know if he's compiling from Qt Creator or not.

                            The point I'm trying to get across is he will need to define some symbol somewhere if he wants to compile for/with Qt or some symbol somewhere if he wants to compile otherwise. QT_IS_AVAILABLE has got to be defined somewhere in those cases where it's wanted, else it'll never any effect. Different makefiles, an environment variable, something so the Qt or non-Qt compilation can be performed.

                            1 Reply Last reply
                            0
                            • K kitfox

                              I'm not modifying ffmpeg. I'm modifying ffmpeg-cpp, a library which wraps ffmpeg and makes it easier to use.

                              B Offline
                              B Offline
                              ben.cottrell
                              wrote on last edited by
                              #14

                              @kitfox What kind of modifications are you doing?

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                kitfox
                                wrote on last edited by
                                #15

                                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
                                
                                K VRoninV 2 Replies Last reply
                                0
                                • K Offline
                                  K Offline
                                  kitfox
                                  wrote on last edited by
                                  #16

                                  Just trying to get the library to build under Qt. Then would like to write a few simple apps to test it to make sure it's working.

                                  1 Reply Last reply
                                  0
                                  • 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
                                    
                                    K Offline
                                    K Offline
                                    kitfox
                                    wrote on last edited by
                                    #17

                                    @kitfox It would be nice if there were some equivalent of IM_USING_QT automatically defined so that I would not have to have all QT users explicitly specify it.

                                    Chris KawaC JonBJ 2 Replies Last reply
                                    0
                                    • K kitfox

                                      @kitfox It would be nice if there were some equivalent of IM_USING_QT automatically defined so that I would not have to have all QT users explicitly specify it.

                                      Chris KawaC Offline
                                      Chris KawaC Offline
                                      Chris Kawa
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      If you're using a decent compiler you can use __has_include to check if Qt headers are available and define some macro constant if they are.
                                      That doesn't solve any linker and toolchain (moc etc.) issues, but that's how you can detect if Qt source is available in your project.

                                      1 Reply Last reply
                                      5
                                      • K kitfox

                                        @kitfox It would be nice if there were some equivalent of IM_USING_QT automatically defined so that I would not have to have all QT users explicitly specify it.

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

                                        @kitfox
                                        If your definition of "I'm using Qt" is you have the Qt include files on your box, @Chris-Kawa's suggestion looks great. Is that what you wanted? Or did you say you wanted to know whether the user is sitting inside Qt Creator? Or does the user choose whether he wants to compile for Qt or not (e.g. like you will want to while testing each route of this code)? What exactly is the deciding factor?

                                        1 Reply Last reply
                                        0
                                        • B Offline
                                          B Offline
                                          ben.cottrell
                                          wrote on last edited by
                                          #20

                                          @kitfox why not use CMake so that you can specifically define whether or not to use Qt, and then decide which implementation source file to add to compilation?

                                          1 Reply Last reply
                                          0

                                          • Login

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