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
Forum Updated to NodeBB v4.3 + New Features

Shared library on windows fails when using moc

Scheduled Pinned Locked Moved Solved General and Desktop
41 Posts 4 Posters 11.0k Views 2 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.
  • MesrineM Mesrine

    Hi everyone,

    I have a library that export a class that derive from QObject,
    The class is called 'nonceFinder'. The class use Q_OBJECT macro on its declaration because uses QT signals.

    The library compiles well for ubuntu, windows, macos for static library and shared library.

    Then I have another library called 'qclient' that uses the previous library 'nonceFinder'.

    The compilation of 'qclient' works for ubuntu, macos for shared and static library, and for windows static library, but fails for windows shared library.
    The error that gives is :

    qclient.cpp.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const qiota::qpow::nonceFinder::staticMetaObject" (?staticMetaObject@nonceFinder@qpow@qiota@@2UQMetaObject@@B) referenced in function "public: static class QMetaObject::Connection __cdecl QObject::connect<void (__cdecl qiota::qpow::nonceFinder::*)(void),class `public: __cdecl `public: __cdecl `public: void __cdecl qiota::Client::send_block(class qiota::qblocks::Block const &)'::`2'::<lambda_2>::operator()(void)const '::`2'::<lambda_1>::operator()(void)const '::`5'::<lambda_2> >(class qiota::qpow::nonceFinder const *,void (__cdecl qiota::qpow::nonceFinder::*)(void),class QObject const *,class `public: __cdecl `public: __cdecl `public: void __cdecl qiota::Client::send_block(class qiota::qblocks::Block const &)'::`2'::<lambda_2>::operator()(void)const '::`2'::<lambda_1>::operator()(void)const '::`5'::<lambda_2>,enum Qt::ConnectionType)" (??$connect@P8nonceFinder@qpow@qiota@@EAAXXZV<lambda_2>@?4???R<lambda_1>@?1???R4?1??send_block@Client@3@QEAAXAEBVBlock@qblocks@3@@Z@QEBA@XZ@QEBA@XZ@@QObject@@SA?AVConnection@QMetaObject@@PEBVnonceFinder@qpow@qiota@@P8345@EAAXXZPEBV0@V<lambda_2>@?4???R<lambda_1>@?1???R6?1??send_block@Client@5@QEAAXAEBVBlock@qblocks@5@@Z@QEBA@XZ@QEBA@XZ@W4ConnectionType@Qt@@@Z)
    

    I have check is a problem with

    Methods like metaObject(), qt_metacall(), qt_metacast(), etc, are defined in the generated moc_ files. So the cause of the unresolved external symbol error is that the moc_ files are not compiled/linked to the project. If, in Visual Studio, deleting and reinserting Q_OBJECT does not work, check that the moc_ files are correctly generated and included in the project.
    

    I have also try https://doc.qt.io/qt-6/sharedlibrary.html macros.
    I use CMake, i do not understand when should I use :

    • Q_DECL_EXPORT - Should be at compilation time of the library?

    • Q_DECL_IMPORT - Should be at linking time of the client and the shared library?

    Also if i am compiling a static libraries can i keep Q_DECL_IMPORT in my class declaration?

    If you have managed to compile a shared library on Windows that uses MOC generated sources I will appreciate your help on this.
    If you now how to set this on CMake better.

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Please show a minimal, compilable example opf your problem. You most likely do not export the classes you want to use from outside correct.

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

    MesrineM 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      Please show a minimal, compilable example opf your problem. You most likely do not export the classes you want to use from outside correct.

      MesrineM Offline
      MesrineM Offline
      Mesrine
      wrote on last edited by Mesrine
      #3

      @Christian-Ehrlicher Thanks for the interest.
      the client library that use the shared library:
      https://github.com/EddyTheCo/Qclient-IOTA
      the shared library:
      https://github.com/EddyTheCo/Qpow-IOTA
      You can open a PR with the solution to develop branch :).

      Christian EhrlicherC 1 Reply Last reply
      0
      • MesrineM Mesrine

        @Christian-Ehrlicher Thanks for the interest.
        the client library that use the shared library:
        https://github.com/EddyTheCo/Qclient-IOTA
        the shared library:
        https://github.com/EddyTheCo/Qpow-IOTA
        You can open a PR with the solution to develop branch :).

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        But there is no shared libary at all nor import/export macros.

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

        MesrineM 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          But there is no shared libary at all nor import/export macros.

          MesrineM Offline
          MesrineM Offline
          Mesrine
          wrote on last edited by
          #5

          @Christian-Ehrlicher
          I tried all combinations of export import macros but did not work either.

          Why my others shared library works on windows without explicit export macros ?
          Why the compilation only fails when using MOC?

          Christian EhrlicherC 1 Reply Last reply
          0
          • MesrineM Mesrine

            @Christian-Ehrlicher
            I tried all combinations of export import macros but did not work either.

            Why my others shared library works on windows without explicit export macros ?
            Why the compilation only fails when using MOC?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

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

            Why my others shared library works on windows without explicit export macros ?

            None will work if you don't properly export the symbols - I don't see any shared library in your github repos

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

            MesrineM 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

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

              Why my others shared library works on windows without explicit export macros ?

              None will work if you don't properly export the symbols - I don't see any shared library in your github repos

              MesrineM Offline
              MesrineM Offline
              Mesrine
              wrote on last edited by Mesrine
              #7

              @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.

              Christian EhrlicherC 1 Reply Last reply
              0
              • MesrineM Mesrine

                @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.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                MesrineM 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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.

                  MesrineM Offline
                  MesrineM Offline
                  Mesrine
                  wrote on last edited by
                  #9

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

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • MesrineM Mesrine

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

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    MesrineM 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @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.

                      MesrineM Offline
                      MesrineM Offline
                      Mesrine
                      wrote on 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.

                      Christian EhrlicherC JoeCFDJ 2 Replies Last reply
                      0
                      • MesrineM Mesrine

                        @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.

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 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
                        • MesrineM Mesrine

                          @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.

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on 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 .

                          Christian EhrlicherC MesrineM 4 Replies Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @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 .

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 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

                            JoeCFDJ 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @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.

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on 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.

                              Christian EhrlicherC 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

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

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 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

                                JoeCFDJ 1 Reply Last reply
                                1
                                • JoeCFDJ JoeCFD

                                  @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 .

                                  MesrineM Offline
                                  MesrineM Offline
                                  Mesrine
                                  wrote on 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.

                                  Christian EhrlicherC JoeCFDJ 2 Replies Last reply
                                  0
                                  • MesrineM Mesrine

                                    @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.

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 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
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @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.

                                      JoeCFDJ Offline
                                      JoeCFDJ Offline
                                      JoeCFD
                                      wrote on 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
                                      
                                      Christian EhrlicherC 1 Reply Last reply
                                      0
                                      • JoeCFDJ JoeCFD

                                        @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
                                        
                                        Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 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

                                        JoeCFDJ 1 Reply Last reply
                                        1
                                        • MesrineM Mesrine

                                          @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.

                                          JoeCFDJ Offline
                                          JoeCFDJ Offline
                                          JoeCFD
                                          wrote on 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

                                          • Login

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