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. Shared library on windows fails when using moc

Shared library on windows fails when using moc

Scheduled Pinned Locked Moved Solved General and Desktop
41 Posts 4 Posters 10.9k Views
  • 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.
  • M Mesrine
    30 May 2023, 16:58

    @Christian-Ehrlicher

    To build shared library you just say to cmake

    qt-cmake -G Ninja -DCMAKE_BUILD_TYPE="release"   -DCMAKE_INSTALL_PREFIX="../install" -DBUILD_SHARED_LIBS=ON ../
    

    All my shared libraries work for windows it only fail when using MOC. The later is not a question is an affirmation.

    C Online
    C Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 30 May 2023, 17:01 last edited by
    #8

    @Mesrine said in Shared library on windows fails when using moc:

    -DBUILD_SHARED_LIBS=ON ..

    I don't see how this should affect any code in e.g. https://github.com/EddyTheCo/Qpow-IOTA/blob/main/CMakeLists.txt apart from the naming of the library.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    M 1 Reply Last reply 30 May 2023, 17:02
    0
    • C Christian Ehrlicher
      30 May 2023, 17:01

      @Mesrine said in Shared library on windows fails when using moc:

      -DBUILD_SHARED_LIBS=ON ..

      I don't see how this should affect any code in e.g. https://github.com/EddyTheCo/Qpow-IOTA/blob/main/CMakeLists.txt apart from the naming of the library.

      M Offline
      M Offline
      Mesrine
      wrote on 30 May 2023, 17:02 last edited by
      #9

      @Christian-Ehrlicher
      Cmake will let know the compiler that I want a shared library.

      C 1 Reply Last reply 30 May 2023, 17:05
      0
      • M Mesrine
        30 May 2023, 17:02

        @Christian-Ehrlicher
        Cmake will let know the compiler that I want a shared library.

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 30 May 2023, 17:05 last edited by
        #10

        @Mesrine said in Shared library on windows fails when using moc:

        Cmake will let know the compiler that I want a shared library.

        ah, correct

        But you should add the correct defines then as described in the Qt documentation.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply 30 May 2023, 17:08
        0
        • C Christian Ehrlicher
          30 May 2023, 17:05

          @Mesrine said in Shared library on windows fails when using moc:

          Cmake will let know the compiler that I want a shared library.

          ah, correct

          But you should add the correct defines then as described in the Qt documentation.

          M Offline
          M Offline
          Mesrine
          wrote on 30 May 2023, 17:08 last edited by
          #11

          @Christian-Ehrlicher
          I have tried but did not work either, maybe is another problem not related with the export macros.

          So if someone has been able to produce shared libraries on Windows while using MOC compiled sources it will be good to know how to do it in CMake.

          C J 2 Replies Last reply 30 May 2023, 17:10
          0
          • M Mesrine
            30 May 2023, 17:08

            @Christian-Ehrlicher
            I have tried but did not work either, maybe is another problem not related with the export macros.

            So if someone has been able to produce shared libraries on Windows while using MOC compiled sources it will be good to know how to do it in CMake.

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 30 May 2023, 17:10 last edited by Christian Ehrlicher
            #12

            @Mesrine said in Shared library on windows fails when using moc:

            I have tried but did not work either, maybe is another problem not related with the export macros.

            Then finally show us what you tried...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • M Mesrine
              30 May 2023, 17:08

              @Christian-Ehrlicher
              I have tried but did not work either, maybe is another problem not related with the export macros.

              So if someone has been able to produce shared libraries on Windows while using MOC compiled sources it will be good to know how to do it in CMake.

              J Offline
              J Offline
              JoeCFD
              wrote on 30 May 2023, 17:45 last edited by JoeCFD
              #13

              @Mesrine It does not matter what you have done. You have to add MYSHAREDLIB_EXPORT into your class definition
              class MYSHAREDLIB_EXPORT MyClass

              But it does not exist in your classes.

              #include <QtCore/QtGlobal>
              
              #if defined(MYSHAREDLIB_LIBRARY)  
              # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
              #else
              #define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
              #endif
              
              

              When you build your lib, add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY). This means your lib exports these classes
              while MYSHAREDLIB_EXPORT=Q_DECL_EXPORT since MYSHAREDLIB_LIBRARY is defined.

              But in your main application, do not add it. However, the headers of all classes in your main application which use the lib have to have MYSHAREDLIB_EXPORT as well. That means these classes import the lib while MYSHAREDLIB_EXPORT=Q_DECL_IMPORT
              since MYSHAREDLIB_LIBRARY is not defined .

              C M 4 Replies Last reply 30 May 2023, 17:51
              0
              • J JoeCFD
                30 May 2023, 17:45

                @Mesrine It does not matter what you have done. You have to add MYSHAREDLIB_EXPORT into your class definition
                class MYSHAREDLIB_EXPORT MyClass

                But it does not exist in your classes.

                #include <QtCore/QtGlobal>
                
                #if defined(MYSHAREDLIB_LIBRARY)  
                # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
                #else
                #define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
                #endif
                
                

                When you build your lib, add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY). This means your lib exports these classes
                while MYSHAREDLIB_EXPORT=Q_DECL_EXPORT since MYSHAREDLIB_LIBRARY is defined.

                But in your main application, do not add it. However, the headers of all classes in your main application which use the lib have to have MYSHAREDLIB_EXPORT as well. That means these classes import the lib while MYSHAREDLIB_EXPORT=Q_DECL_IMPORT
                since MYSHAREDLIB_LIBRARY is not defined .

                C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 30 May 2023, 17:51 last edited by
                #14

                @JoeCFD said in Shared library on windows fails when using moc:

                #if defined(_WIN32) || defined(WIN32)

                This is not needed and even counter-productive when you want to hide the symbols on linux.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 30 May 2023, 17:53
                0
                • C Christian Ehrlicher
                  30 May 2023, 17:51

                  @JoeCFD said in Shared library on windows fails when using moc:

                  #if defined(_WIN32) || defined(WIN32)

                  This is not needed and even counter-productive when you want to hide the symbols on linux.

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 30 May 2023, 17:53 last edited by JoeCFD
                  #15

                  @Christian-Ehrlicher Got it, thanks. Q_DECL_EXPORT and Q_DECL_IMPORT have it already. But for non Qt code, this is needed.

                  C 1 Reply Last reply 30 May 2023, 17:56
                  0
                  • J JoeCFD
                    30 May 2023, 17:53

                    @Christian-Ehrlicher Got it, thanks. Q_DECL_EXPORT and Q_DECL_IMPORT have it already. But for non Qt code, this is needed.

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 30 May 2023, 17:56 last edited by
                    #16

                    @JoeCFD said in Shared library on windows fails when using moc:

                    But for non Qt code, this is needed.

                    Even then I would use the correct attribution. Hiding the symbols by default on linux will help the linker and startup time and (iirc) also the optimizer in the linker since it can remove unused functions / inline stuff when it's not visible from outside.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    J 1 Reply Last reply 30 May 2023, 18:01
                    1
                    • J JoeCFD
                      30 May 2023, 17:45

                      @Mesrine It does not matter what you have done. You have to add MYSHAREDLIB_EXPORT into your class definition
                      class MYSHAREDLIB_EXPORT MyClass

                      But it does not exist in your classes.

                      #include <QtCore/QtGlobal>
                      
                      #if defined(MYSHAREDLIB_LIBRARY)  
                      # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
                      #else
                      #define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
                      #endif
                      
                      

                      When you build your lib, add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY). This means your lib exports these classes
                      while MYSHAREDLIB_EXPORT=Q_DECL_EXPORT since MYSHAREDLIB_LIBRARY is defined.

                      But in your main application, do not add it. However, the headers of all classes in your main application which use the lib have to have MYSHAREDLIB_EXPORT as well. That means these classes import the lib while MYSHAREDLIB_EXPORT=Q_DECL_IMPORT
                      since MYSHAREDLIB_LIBRARY is not defined .

                      M Offline
                      M Offline
                      Mesrine
                      wrote on 30 May 2023, 17:57 last edited by
                      #17

                      @JoeCFD said in Shared library on windows fails when using moc:

                      MYSHAREDLIB_LIBRARY

                      Yes, I tried that and give the same error.

                      Then my question is when should i define MYSHAREDLIB_LIBRARY in cmake?
                      When compiling the library?
                      If compiling the client should i set MYSHAREDLIB_LIBRARY?

                      In my case compiling the client download the source code of the library and compile the source code.
                      The target resulting from that shared library is linked to the client using cmake .

                      Maybe i found my problem while writing this.
                      I will do what Qt says(exactly :)) and try again, I llet you know the result and the code.

                      Thanks.

                      C J 2 Replies Last reply 30 May 2023, 17:57
                      0
                      • M Mesrine
                        30 May 2023, 17:57

                        @JoeCFD said in Shared library on windows fails when using moc:

                        MYSHAREDLIB_LIBRARY

                        Yes, I tried that and give the same error.

                        Then my question is when should i define MYSHAREDLIB_LIBRARY in cmake?
                        When compiling the library?
                        If compiling the client should i set MYSHAREDLIB_LIBRARY?

                        In my case compiling the client download the source code of the library and compile the source code.
                        The target resulting from that shared library is linked to the client using cmake .

                        Maybe i found my problem while writing this.
                        I will do what Qt says(exactly :)) and try again, I llet you know the result and the code.

                        Thanks.

                        C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 30 May 2023, 17:57 last edited by
                        #18

                        @Mesrine said in Shared library on windows fails when using moc:

                        Then my question is when should i define MYSHAREDLIB_LIBRARY in cmake?
                        When compiling the library?

                        add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY)

                        And instead 'MYSHAREDLIB' you should use your library name...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • C Christian Ehrlicher
                          30 May 2023, 17:56

                          @JoeCFD said in Shared library on windows fails when using moc:

                          But for non Qt code, this is needed.

                          Even then I would use the correct attribution. Hiding the symbols by default on linux will help the linker and startup time and (iirc) also the optimizer in the linker since it can remove unused functions / inline stuff when it's not visible from outside.

                          J Offline
                          J Offline
                          JoeCFD
                          wrote on 30 May 2023, 18:01 last edited by
                          #19

                          @Christian-Ehrlicher
                          Good to know, Thanks. I have not used Windows for ages.

                            #if __GNUC__ >= 4
                              #define DLL_PUBLIC __attribute__ ((visibility ("default")))
                              #define DLL_LOCAL  __attribute__ ((visibility ("hidden")))
                            #else
                              #define DLL_PUBLIC
                              #define DLL_LOCAL
                            #endif
                          
                          C 1 Reply Last reply 30 May 2023, 18:05
                          0
                          • J JoeCFD
                            30 May 2023, 18:01

                            @Christian-Ehrlicher
                            Good to know, Thanks. I have not used Windows for ages.

                              #if __GNUC__ >= 4
                                #define DLL_PUBLIC __attribute__ ((visibility ("default")))
                                #define DLL_LOCAL  __attribute__ ((visibility ("hidden")))
                              #else
                                #define DLL_PUBLIC
                                #define DLL_LOCAL
                              #endif
                            
                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 30 May 2023, 18:05 last edited by
                            #20

                            @JoeCFD You have then compile with -fvisibility-inlines-hidden -fvisibility=hiddenand get the same behavior like the (default) windows behavior. See CMAKE_CXX_VISIBILITY_PRESET for more information on how to use it platform independent with cmake.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            J 1 Reply Last reply 30 May 2023, 18:07
                            1
                            • M Mesrine
                              30 May 2023, 17:57

                              @JoeCFD said in Shared library on windows fails when using moc:

                              MYSHAREDLIB_LIBRARY

                              Yes, I tried that and give the same error.

                              Then my question is when should i define MYSHAREDLIB_LIBRARY in cmake?
                              When compiling the library?
                              If compiling the client should i set MYSHAREDLIB_LIBRARY?

                              In my case compiling the client download the source code of the library and compile the source code.
                              The target resulting from that shared library is linked to the client using cmake .

                              Maybe i found my problem while writing this.
                              I will do what Qt says(exactly :)) and try again, I llet you know the result and the code.

                              Thanks.

                              J Offline
                              J Offline
                              JoeCFD
                              wrote on 30 May 2023, 18:06 last edited by JoeCFD
                              #21

                              @Mesrine Take a look at the source code of any Qt module. You do the same thing.

                              1 Reply Last reply
                              0
                              • C Christian Ehrlicher
                                30 May 2023, 18:05

                                @JoeCFD You have then compile with -fvisibility-inlines-hidden -fvisibility=hiddenand get the same behavior like the (default) windows behavior. See CMAKE_CXX_VISIBILITY_PRESET for more information on how to use it platform independent with cmake.

                                J Offline
                                J Offline
                                JoeCFD
                                wrote on 30 May 2023, 18:07 last edited by
                                #22

                                @Christian-Ehrlicher Thanks. Learn more things from you.

                                1 Reply Last reply
                                0
                                • J JoeCFD
                                  30 May 2023, 17:45

                                  @Mesrine It does not matter what you have done. You have to add MYSHAREDLIB_EXPORT into your class definition
                                  class MYSHAREDLIB_EXPORT MyClass

                                  But it does not exist in your classes.

                                  #include <QtCore/QtGlobal>
                                  
                                  #if defined(MYSHAREDLIB_LIBRARY)  
                                  # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
                                  #else
                                  #define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
                                  #endif
                                  
                                  

                                  When you build your lib, add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY). This means your lib exports these classes
                                  while MYSHAREDLIB_EXPORT=Q_DECL_EXPORT since MYSHAREDLIB_LIBRARY is defined.

                                  But in your main application, do not add it. However, the headers of all classes in your main application which use the lib have to have MYSHAREDLIB_EXPORT as well. That means these classes import the lib while MYSHAREDLIB_EXPORT=Q_DECL_IMPORT
                                  since MYSHAREDLIB_LIBRARY is not defined .

                                  M Offline
                                  M Offline
                                  Mesrine
                                  wrote on 30 May 2023, 18:21 last edited by Mesrine
                                  #23

                                  @JoeCFD said in Shared library on windows fails when using moc:

                                  MYSHAREDLIB_EXPORT

                                  But still i have to define MYSHAREDLIB_EXPORT to nothing if compiling in other system apart from windows?

                                  J C 2 Replies Last reply 30 May 2023, 18:32
                                  0
                                  • J JoeCFD
                                    30 May 2023, 17:45

                                    @Mesrine It does not matter what you have done. You have to add MYSHAREDLIB_EXPORT into your class definition
                                    class MYSHAREDLIB_EXPORT MyClass

                                    But it does not exist in your classes.

                                    #include <QtCore/QtGlobal>
                                    
                                    #if defined(MYSHAREDLIB_LIBRARY)  
                                    # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
                                    #else
                                    #define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
                                    #endif
                                    
                                    

                                    When you build your lib, add target_compile_definitions(mysharedlib PRIVATE MYSHAREDLIB_LIBRARY). This means your lib exports these classes
                                    while MYSHAREDLIB_EXPORT=Q_DECL_EXPORT since MYSHAREDLIB_LIBRARY is defined.

                                    But in your main application, do not add it. However, the headers of all classes in your main application which use the lib have to have MYSHAREDLIB_EXPORT as well. That means these classes import the lib while MYSHAREDLIB_EXPORT=Q_DECL_IMPORT
                                    since MYSHAREDLIB_LIBRARY is not defined .

                                    M Offline
                                    M Offline
                                    Mesrine
                                    wrote on 30 May 2023, 18:25 last edited by
                                    #24

                                    @JoeCFD

                                    grep on the sources of qt gives:

                                    grep -r "Q_DECL_IMPORT"
                                    qcompilerdetection.qdoc:    \sa Q_DECL_IMPORT
                                    qcompilerdetection.qdoc:    \macro Q_DECL_IMPORT
                                    qtconfigmacros.h:#    define Q_AUTOTEST_EXPORT Q_DECL_IMPORT
                                    qversiontagging.h:    extern "C" Q_DECL_IMPORT const char sym; \
                                    qcompilerdetection.h:#  define Q_DECL_IMPORT __declspec(dllimport)
                                    qcompilerdetection.h:#    define Q_DECL_IMPORT     __attribute__((visibility("default")))
                                    qcompilerdetection.h:#    define Q_DECL_IMPORT     __declspec(dllimport)
                                    qcompilerdetection.h:#    define Q_DECL_IMPORT     __declspec(dllimport)
                                    qcompilerdetection.h:#    define Q_DECL_IMPORT     __attribute__((visibility("default")))
                                    qcompilerdetection.h:#ifndef Q_DECL_IMPORT
                                    qcompilerdetection.h:#  define Q_DECL_IMPORT
                                    
                                    1 Reply Last reply
                                    0
                                    • M Mesrine
                                      30 May 2023, 18:21

                                      @JoeCFD said in Shared library on windows fails when using moc:

                                      MYSHAREDLIB_EXPORT

                                      But still i have to define MYSHAREDLIB_EXPORT to nothing if compiling in other system apart from windows?

                                      J Offline
                                      J Offline
                                      JoeCFD
                                      wrote on 30 May 2023, 18:32 last edited by
                                      #25

                                      @Mesrine Q_DECL_IMPORT and Q_DECL_EXPORT are hidden for example on Linux. No worries.

                                      1 Reply Last reply
                                      0
                                      • M Mesrine
                                        30 May 2023, 18:21

                                        @JoeCFD said in Shared library on windows fails when using moc:

                                        MYSHAREDLIB_EXPORT

                                        But still i have to define MYSHAREDLIB_EXPORT to nothing if compiling in other system apart from windows?

                                        C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 30 May 2023, 18:36 last edited by
                                        #26

                                        @Mesrine said in Shared library on windows fails when using moc:

                                        But still i have to define MYSHAREDLIB_EXPORT to nothing if compiling in other system apart from windows?

                                        Why? Simply use it as explained in the Qt documentation...

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        SGaistS 1 Reply Last reply 30 May 2023, 18:41
                                        0
                                        • C Christian Ehrlicher
                                          30 May 2023, 18:36

                                          @Mesrine said in Shared library on windows fails when using moc:

                                          But still i have to define MYSHAREDLIB_EXPORT to nothing if compiling in other system apart from windows?

                                          Why? Simply use it as explained in the Qt documentation...

                                          SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 30 May 2023, 18:41 last edited by
                                          #27

                                          Hi,

                                          You are over complicating things.

                                          As already suggested by @Christian-Ehrlicher, just follow the doc:

                                          • create one header with the macro definition as shown in the documentation
                                          • in your code include that header in all the classes you want to export and use the macro there
                                          • update your CMakeLists.txt as shown
                                          • rebuild

                                          And you should be good to go. There's really nothing more to it than that.

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

                                          M 1 Reply Last reply 30 May 2023, 19:27
                                          0

                                          17/41

                                          30 May 2023, 17:57

                                          • Login

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