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. How can I use a DLL, in VS, without needing to load the moc files from the DLL?
Forum Updated to NodeBB v4.3 + New Features

How can I use a DLL, in VS, without needing to load the moc files from the DLL?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 5.4k Views 3 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.
  • AEROLASER_THAA AEROLASER_THA

    @kshegunov

    #ifndef AECREATORMENU_H
    #define AECREATORMENU_H
    
    #include "aebasecreatormenu.h"
    
    template <class T> class AECREATOR_EXPORT  AeCreatorMenu : public AeBaseCreatorMenu
    {
    public:
    	explicit AeCreatorMenu(const QString &creatorName, QObject *parent = 0)	: AeBaseCreatorMenu(creatorName, parent){};
    	~AeCreatorMenu(){};
    
    	QMenu* createMenu(QWidget *parent = 0)
    	{
    		return static_cast<QMenu*>(new T(parent));
    	}
    
    private:
    	Q_DISABLE_COPY(AeCreatorMenu);
    };
    
    #endif // AECREATORMENU_H
    

    [Added code tags ~kshegunov]

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #11

    Hi
    You are trying to export a template which wont work as class T is not yet known.
    As far as I know, you can only export an instantiation where T is set to a type.

    1 Reply Last reply
    1
    • AEROLASER_THAA AEROLASER_THA

      @kshegunov

      #ifndef AECREATORMENU_H
      #define AECREATORMENU_H
      
      #include "aebasecreatormenu.h"
      
      template <class T> class AECREATOR_EXPORT  AeCreatorMenu : public AeBaseCreatorMenu
      {
      public:
      	explicit AeCreatorMenu(const QString &creatorName, QObject *parent = 0)	: AeBaseCreatorMenu(creatorName, parent){};
      	~AeCreatorMenu(){};
      
      	QMenu* createMenu(QWidget *parent = 0)
      	{
      		return static_cast<QMenu*>(new T(parent));
      	}
      
      private:
      	Q_DISABLE_COPY(AeCreatorMenu);
      };
      
      #endif // AECREATORMENU_H
      

      [Added code tags ~kshegunov]

      AEROLASER_THAA Offline
      AEROLASER_THAA Offline
      AEROLASER_THA
      wrote on last edited by
      #12

      @AEROLASER_THA
      Dear Kshegunov;
      The problem is with any class, regardless of whether it is abstract or not. With any simple project and class.

      If I am actually declaring AECREATOR_LIB, in the project AeCreator_test.exe

      Preprocessor:
      UNICODE; WIN64; QT_DLL; QT_NO_DEBUG; NDEBUG; QT_CORE_LIB; QT_GUI_LIB; QT_WIDGETS_LIB; AECREATOR_LIB;% (PreprocessorDefinitions)

      mrjjM kshegunovK 2 Replies Last reply
      0
      • AEROLASER_THAA AEROLASER_THA

        @AEROLASER_THA
        Dear Kshegunov;
        The problem is with any class, regardless of whether it is abstract or not. With any simple project and class.

        If I am actually declaring AECREATOR_LIB, in the project AeCreator_test.exe

        Preprocessor:
        UNICODE; WIN64; QT_DLL; QT_NO_DEBUG; NDEBUG; QT_CORE_LIB; QT_GUI_LIB; QT_WIDGETS_LIB; AECREATOR_LIB;% (PreprocessorDefinitions)

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #13

        @AEROLASER_THA
        Hi
        Did you try something simple like
        https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

        It shows the step both for the DLL and for the host app.

        AEROLASER_THAA 1 Reply Last reply
        1
        • AEROLASER_THAA AEROLASER_THA

          @AEROLASER_THA
          Dear Kshegunov;
          The problem is with any class, regardless of whether it is abstract or not. With any simple project and class.

          If I am actually declaring AECREATOR_LIB, in the project AeCreator_test.exe

          Preprocessor:
          UNICODE; WIN64; QT_DLL; QT_NO_DEBUG; NDEBUG; QT_CORE_LIB; QT_GUI_LIB; QT_WIDGETS_LIB; AECREATOR_LIB;% (PreprocessorDefinitions)

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

          @AEROLASER_THA said in How can I use a DLL, in VS, without needing to load the moc files from the DLL?:

          If I am actually declaring AECREATOR_LIB, in the project AeCreator_test.exe

          Which I said you shouldn't! You should define that only when building the library, not when using the library, otherwise it will not expand to __declspec(dllimport) which is mandatory on windows.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • mrjjM mrjj

            @AEROLASER_THA
            Hi
            Did you try something simple like
            https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

            It shows the step both for the DLL and for the host app.

            AEROLASER_THAA Offline
            AEROLASER_THAA Offline
            AEROLASER_THA
            wrote on last edited by kshegunov
            #15

            @mrjj

            //DLL test

            //test.h

            #include "test_global.h"
            
            #include <QtWidgets/QWidget>
            
            class TEST_EXPORT Widget : public QWidget
            {
            	Q_OBJECT
            
            public:
                Widget();
            };
            

            //test_global.h

            #include <QtCore/qglobal.h>
            
            #ifndef QT_STATIC
            # if defined(TEST_LIB)
            #  define TEST_EXPORT Q_DECL_EXPORT
            # else
            #  define TEST_EXPORT Q_DECL_IMPORT
            # endif
            #else
            # define TEST_EXPORT
            #endif
            
            //test.cpp
            #include "test.h"
            
            Widget::Widget()
            {
            
            }
            

            //PROJECT

            #include <QtWidgets/QApplication>
            
            #include "test.h"
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            	Widget w;
            	w.resize(100, 100);
            	w.show();
            	return a.exec();
            }
            

            //CONFIFGURATION
            Additional Inclusion Directories;
            .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtWidgets;..\test;..\test\GeneratedFiles$(ConfigurationName);%(AdditionalIncludeDirectories)

            Preprocessor;
            UNICODE;WIN32;WIN64;QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;TEST_LIB;%(PreprocessorDefinitions)

            Directory of additional libraries;
            $(QTDIR)\lib;$(SolutionDir)$(Platform)$(Configuration);%(AdditionalLibraryDirectories)
            Linker;
            qtmaind.lib;Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;test.lib;%(AdditionalDependencies)

            If I do not load the moc_test.cpp file generated with the DLL, the following error occurs in the exe project manually;

            main.obj : error LNK2001: símbolo externo "public: static struct QMetaObject const Widget::staticMetaObject" (?staticMetaObject@Widget@@2UQMetaObject@@B) sin resolver
            1>c:\Users\therrera\documents\visual studio 2015\Projects\test\x64\Debug\Project.exe : fatal error LNK1120: 1 externos sin resolver

            If you manually load the m_test.cpp file, in the project, in the GeneratedFiles folder, it runs perfectly.
            It is as if the moc code was not contained in the DLL

            [Added code tags ~kshegunov]

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #16

              Hi
              It sounds like
              if defined(TEST_LIB)
              define TEST_EXPORT Q_DECL_EXPORT
              is active when you build the app ?

              In sample the key points are
              TEMPLATE = lib <<<
              DEFINES += TEST<<<
              for the LIB, not app.

              So how are you controlling the TEST_LIB when building the lib and the app ?
              for the app, TEST_LIB is not to be defined.

              AEROLASER_THAA 1 Reply Last reply
              2
              • mrjjM mrjj

                Hi
                It sounds like
                if defined(TEST_LIB)
                define TEST_EXPORT Q_DECL_EXPORT
                is active when you build the app ?

                In sample the key points are
                TEMPLATE = lib <<<
                DEFINES += TEST<<<
                for the LIB, not app.

                So how are you controlling the TEST_LIB when building the lib and the app ?
                for the app, TEST_LIB is not to be defined.

                AEROLASER_THAA Offline
                AEROLASER_THAA Offline
                AEROLASER_THA
                wrote on last edited by
                #17

                @mrjj
                Yes, is active

                //DLL
                Preprocessor;
                UNICODE;WIN32;WIN64;QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;TEST_LIB;%(PreprocessorDefinitions)
                Linker;
                qtmaind.lib;Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)

                //PROJECT
                Preprocessor;
                UNICODE;WIN32;WIN64;QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;TEST_LIB;%(PreprocessorDefinitions)
                Linker;
                qtmaind.lib;Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;test.lib;%(AdditionalDependencies)

                But it only runs correctly, if in the project manually load the moc_test.cpp

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #18

                  So you are 100% sure that its Q_DECL_IMPORT when building the app and
                  Q_DECL_EXPORT when building the LIB/DLL ?
                  Sorry I cannot see it from your output.
                  Both seems to say TEST_LIB; which is wrong.
                  Only
                  //DLL
                  Preprocessor;
                  should have it.

                  AEROLASER_THAA 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    So you are 100% sure that its Q_DECL_IMPORT when building the app and
                    Q_DECL_EXPORT when building the LIB/DLL ?
                    Sorry I cannot see it from your output.
                    Both seems to say TEST_LIB; which is wrong.
                    Only
                    //DLL
                    Preprocessor;
                    should have it.

                    AEROLASER_THAA Offline
                    AEROLASER_THAA Offline
                    AEROLASER_THA
                    wrote on last edited by
                    #19

                    @mrjj

                    TEST_LIB is declared in both DLL and Project

                    If I do not declare in Project, in preprocessor, it gives me error.

                    moc_test.cpp
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(56): warning C4273: 'Widget::qt_static_metacall': vinculación de DLL incoherente
                    1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_static_metacall'
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(63): warning C4273: 'staticMetaObject': vinculación de DLL incoherente
                    1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'public: static QMetaObject const Widget::staticMetaObject'
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(63): error C2491: 'Widget::staticMetaObject': definición de miembro de datos estático dllimport no permitida
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(70): warning C4273: 'Widget::metaObject': vinculación de DLL incoherente
                    1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'metaObject'
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(75): warning C4273: 'Widget::qt_metacast': vinculación de DLL incoherente
                    1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_metacast'
                    1>..\test\GeneratedFiles\Debug\moc_test.cpp(83): warning C4273: 'Widget::qt_metacall': vinculación de DLL incoherente
                    1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_metacall'
                    1> main.cpp

                    mrjjM 1 Reply Last reply
                    0
                    • AEROLASER_THAA AEROLASER_THA

                      @mrjj

                      TEST_LIB is declared in both DLL and Project

                      If I do not declare in Project, in preprocessor, it gives me error.

                      moc_test.cpp
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(56): warning C4273: 'Widget::qt_static_metacall': vinculación de DLL incoherente
                      1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_static_metacall'
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(63): warning C4273: 'staticMetaObject': vinculación de DLL incoherente
                      1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'public: static QMetaObject const Widget::staticMetaObject'
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(63): error C2491: 'Widget::staticMetaObject': definición de miembro de datos estático dllimport no permitida
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(70): warning C4273: 'Widget::metaObject': vinculación de DLL incoherente
                      1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'metaObject'
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(75): warning C4273: 'Widget::qt_metacast': vinculación de DLL incoherente
                      1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_metacast'
                      1>..\test\GeneratedFiles\Debug\moc_test.cpp(83): warning C4273: 'Widget::qt_metacall': vinculación de DLL incoherente
                      1> c:\users\therrera\documents\visual studio 2015\projects\test\test\generatedfiles\debug../../test.h(10): note: vea la definición anterior de 'qt_metacall'
                      1> main.cpp

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      @AEROLASER_THA
                      hmm. ok. im going to try the small test project and see.
                      You added a UI files also ?

                      1 Reply Last reply
                      0
                      • AEROLASER_THAA Offline
                        AEROLASER_THAA Offline
                        AEROLASER_THA
                        wrote on last edited by
                        #21

                        In this example, I am not using ui files

                        AEROLASER_THAA 1 Reply Last reply
                        0
                        • AEROLASER_THAA AEROLASER_THA

                          In this example, I am not using ui files

                          AEROLASER_THAA Offline
                          AEROLASER_THAA Offline
                          AEROLASER_THA
                          wrote on last edited by AEROLASER_THA
                          #22

                          @AEROLASER_THA

                          Dear All;
                          I finally found the problem.
                          I was creating a DLL project, and within this project I created the EXE project.
                          When declaring the path of the inclusion directories of the EXE project, it was not correctly including the address of the path where the lib

                          And indeed my initial confusion was due to the fact of trying to export a template as a class T that is not known.

                          Excuse me

                          .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtWidgets;..\test;..\test\GeneratedFiles$(ConfigurationName);C:\Users\therrera\Documents\Visual Studio 2015\Projects\test\x64\Debug;C:\Users\therrera\Documents\Visual Studio 2015\Projects\test\test\x64\Debug;C:\Users\therrera\Documents\Visual Studio 2015\Projects\test\test\GeneratedFiles\Debug;C:\Users\therrera\Documents\Visual Studio 2015\Projects\test\test;%(AdditionalIncludeDirectories)

                          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