Congratulations to our 2022 Qt Champions!

Export C++ class from Qt Library on Windows

  • 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)





    in QtSharedLib.h:

    @#include "QtSharedLib_global.h"

    int Sub(int a, int b);

    in QtSharedLib.h:

    @include "QtSharedLib.h"

    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.

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

  • 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]

  • I have tried:

    Change 'Add' to 'MyAdd'

    Remove QTSHAREDLIBSHARED_EXPORT from the .cpp file.

    But same error.

  • Finally I got it!

    the dll library project folder:

    the build folder:

    the widget application project folder:

    the widget application build folder:

    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:

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

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

Log in to reply