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. VS Tools how to use a qt class library

VS Tools how to use a qt class library

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 628 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.
  • A Offline
    A Offline
    astoffregen
    wrote on 13 Feb 2023, 06:32 last edited by
    #1

    I am trying to work on qt class libraries and i want to include a qt class library in a qt application. Platform is visual studio 2022, qt tools and qt 6.4.1. application and class library I created via project wizard.
    I have included the class library via links and added the path to the header and library path in the QT project settings.
    I can include the header file but nothing from the library is visible except the default constructor.

    VS Project:
    project.png

    QT Project settings
    libraries.png

    Testlib.h

    #pragma once
    #include "testlib_global.h"
    
    class TESTLIB_EXPORT TestLib
    {
        public:
        TestLib();
        void SayHello();
    };
    

    Testlib_global.h

    #pragma once
    
    #include <QtCore/qglobal.h>
    
    #ifndef BUILD_STATIC
    # if defined(TESTLIB_LIB)
    #  define TESTLIB_EXPORT Q_DECL_EXPORT
    # else
    #  define TESTLIB_EXPORT Q_DECL_IMPORT
    # endif
    #else
    # define TESTLIB_EXPORT
    #endif
    

    Testlib.ccp

    #include "TestLib.h"
    
    TestLib::TestLib()
    {
    }
    
    void TestLib::SayHello()
    {
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtCore/qglobal.h>
    #include "testlib.h"
    
    int main(int argc, char *argv[])
    {
    #if defined(Q_OS_WIN)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        TestLib* lib = new TestLib();
        lib->SayHello();
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    lib->SayHello() gives an error because nothing is found in lib except the constructor

    J 1 Reply Last reply 13 Feb 2023, 08:18
    0
    • A astoffregen
      13 Feb 2023, 06:32

      I am trying to work on qt class libraries and i want to include a qt class library in a qt application. Platform is visual studio 2022, qt tools and qt 6.4.1. application and class library I created via project wizard.
      I have included the class library via links and added the path to the header and library path in the QT project settings.
      I can include the header file but nothing from the library is visible except the default constructor.

      VS Project:
      project.png

      QT Project settings
      libraries.png

      Testlib.h

      #pragma once
      #include "testlib_global.h"
      
      class TESTLIB_EXPORT TestLib
      {
          public:
          TestLib();
          void SayHello();
      };
      

      Testlib_global.h

      #pragma once
      
      #include <QtCore/qglobal.h>
      
      #ifndef BUILD_STATIC
      # if defined(TESTLIB_LIB)
      #  define TESTLIB_EXPORT Q_DECL_EXPORT
      # else
      #  define TESTLIB_EXPORT Q_DECL_IMPORT
      # endif
      #else
      # define TESTLIB_EXPORT
      #endif
      

      Testlib.ccp

      #include "TestLib.h"
      
      TestLib::TestLib()
      {
      }
      
      void TestLib::SayHello()
      {
      }
      

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QtCore/qglobal.h>
      #include "testlib.h"
      
      int main(int argc, char *argv[])
      {
      #if defined(Q_OS_WIN)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QGuiApplication app(argc, argv);
      
          TestLib* lib = new TestLib();
          lib->SayHello();
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      

      lib->SayHello() gives an error because nothing is found in lib except the constructor

      J Offline
      J Offline
      JonB
      wrote on 13 Feb 2023, 08:18 last edited by JonB
      #2

      @astoffregen
      The expansion of TESTLIB_EXPORT when Testlib.h is read depends on the existence/definition of TESTLIB_LIB at the time it is read (unless you are building with BUILD_STATIC which I imagine you are not). Isn't the idea that your Testlib.cpp (only this file) should have #define TESTLIB_LIB (to say this is the implementation) somewhere before it goes #include "TestLib.h"? Does that affect your situation?

      A 1 Reply Last reply 13 Feb 2023, 10:52
      0
      • J JonB
        13 Feb 2023, 08:18

        @astoffregen
        The expansion of TESTLIB_EXPORT when Testlib.h is read depends on the existence/definition of TESTLIB_LIB at the time it is read (unless you are building with BUILD_STATIC which I imagine you are not). Isn't the idea that your Testlib.cpp (only this file) should have #define TESTLIB_LIB (to say this is the implementation) somewhere before it goes #include "TestLib.h"? Does that affect your situation?

        A Offline
        A Offline
        astoffregen
        wrote on 13 Feb 2023, 10:52 last edited by
        #3

        @JonB I still changed the library type to static, because I don't need a dynamic library. The method can now be selected in the main.cpp. but when compiling comes these errors:

        lib3.png

        Severity Code Description Project File Line Suppression State
        Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: __cdecl TestLib::TestLib(void)" (__imp_??0TestLib@@QEAA@XZ)" in function "main".	TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
        
        Severity Code Description Project File Line Suppression state
        Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: void __cdecl TestLib::sayHello(void)" (__imp_?sayHello@TestLib@@QEAAXXZ)" in function "main".	TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
        
        Two unresolved external
        

        Probably this is a beginner's problem - I'm just switching from c# to c++ due to the project and it's much more complex.

        Since the application itself is already very complex i wanted to at least create the structures correctly right at the beginning and for that i need several libraries

        Thanks for any support

        J 1 Reply Last reply 13 Feb 2023, 14:43
        0
        • A astoffregen
          13 Feb 2023, 10:52

          @JonB I still changed the library type to static, because I don't need a dynamic library. The method can now be selected in the main.cpp. but when compiling comes these errors:

          lib3.png

          Severity Code Description Project File Line Suppression State
          Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: __cdecl TestLib::TestLib(void)" (__imp_??0TestLib@@QEAA@XZ)" in function "main".	TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
          
          Severity Code Description Project File Line Suppression state
          Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: void __cdecl TestLib::sayHello(void)" (__imp_?sayHello@TestLib@@QEAAXXZ)" in function "main".	TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
          
          Two unresolved external
          

          Probably this is a beginner's problem - I'm just switching from c# to c++ due to the project and it's much more complex.

          Since the application itself is already very complex i wanted to at least create the structures correctly right at the beginning and for that i need several libraries

          Thanks for any support

          J Offline
          J Offline
          JonB
          wrote on 13 Feb 2023, 14:43 last edited by
          #4

          @astoffregen Looks to me like you're not linking against TestLib.lib.

          1 Reply Last reply
          0

          3/4

          13 Feb 2023, 10:52

          • Login

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