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. Export C++ class from Qt Library on Windows
Forum Update on Monday, May 27th 2025

Export C++ class from Qt Library on Windows

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 10.6k 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.
  • K Offline
    K Offline
    kathy
    wrote on last edited by
    #1

    I have created a Qt shared library project using Qt Creator 2.1 (vc 2008 compiler) and Qt Library 4.7.3 on Windows.

    in QtSharedLib_global.h:

    @#if defined(QTSHAREDLIB_LIBRARY)

    define QTSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT

    #else

    define QTSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT

    #endif@

    in QtSharedLib.h:

    @#include "QtSharedLib_global.h"

    class QTSHAREDLIBSHARED_EXPORT QtSharedLib {
    public:
    QtSharedLib();
    int Sub(int a, int b);
    };@

    in QtSharedLib.h:

    @include "QtSharedLib.h"

    QtSharedLib::QtSharedLib()
    {
    }
    int QtSharedLib::Sub(int a, int b)
    {
    return (a-b);
    }@

    I can build it successfully.

    In my widget application. in MainWindow.cpp funtion - on_pushButton_clicked():

    @ QtSharedLib *lib = new QtSharedLib();
    int c = lib->Sub(4, 3);@

    in the .pro:
    @QT += core gui
    TARGET = Qt_WidgetApp
    TEMPLATE = app
    SOURCES += main.cpp MainWindow.cpp
    HEADERS += MainWindow.h
    FORMS += MainWindow.ui
    INCLUDEPATH += ../QtSharedLib

    LIBS += C:/Qt_Test/QtSharedLib-build-desktop/debug/QtSharedLib.lib@

    I can build it successfully. But when I run it, I got popup dialog say exception triggered: The inferior stopped because it triggered an exception. Stopped in thread 0...

    What is the problem?

    I can import / export functions. But do not know how to inport / export class.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kathy
      wrote on last edited by
      #2

      OK. I figure out something.

      I forget to mention something. In the library project, I also have: FunctionsLib.h/cpp

      in FunctionsLib.h
      @#include "QtSharedLib_global.h"
      QTSHAREDLIBSHARED_EXPORT int Add(int a, int b);@

      in FunctionsLib.cpp
      @#include "FunctionsLib.h"
      QTSHAREDLIBSHARED_EXPORT int Add(int a, int b)
      {
      return (a+b);
      }@

      If I remove the FunctionsLib.h/cpp from library. Then build both dll and widget application project OK and Run application no error.

      So question is:

      Can I include multiple h/cpp files in the library? If yes, how to do it? if no, then how to export multiple classes? I like one file for one class.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        Yes, you can have severeal .h/.cpp files in one library and you can export many symbols, too.

        Try giving "Add" another name or putting it into a namespace. Maybe some other library is already exporting a "Add"-symbol and you get a conflict... which will be resolved one way or the other with only one of the implementations being used. That can trigger all kinds of strange effects:)

        In general you only need to export/import a symbol in the header file, too, so consider to remove the QTSHAREDLIBSHARED_EXPORT from the .cpp file.

        [EDIT: fixed strike-through markup, Volker]

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kathy
          wrote on last edited by
          #4

          I have tried:

          Change 'Add' to 'MyAdd'

          Remove QTSHAREDLIBSHARED_EXPORT from the .cpp file.

          But same error.

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

            Finally I got it!

            the dll library project folder:
            C:/Qt_Test/QtSharedLib

            the build folder:
            C:/Qt_Test/QtSharedLib-build-desktop

            the widget application project folder:
            C:/Qt_Test/Qt_WidgetApp

            the widget application build folder:
            C:/Qt_Test/Qt_WidgetApp-build-desktop

            In the widget application .pro file:
            LIBS += C:/Qt_Test/QtSharedLib-build-desktop/debug/QtSharedLib.lib

            If I copy the dll file to the folder:
            C:/Qt_Test/Qt_WidgetApp-build-desktop/debug

            So question is: how to set dll path in .pro file?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kathy
              wrote on last edited by
              #6

              It seems no way to set dll library path inside .pro. Anyway, I could set PATH in windows environment so that exe will use to find the dll path.

              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