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. Create and use dll in Qt Creator on Windows
Forum Updated to NodeBB v4.3 + New Features

Create and use dll in Qt Creator on Windows

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

    I would like to create a dll and use the dll in Qt Creator on Windows XP. My Qt Creator use the VC 2008 compiler.

    First, I use Creator to create a shared library –

    I have create 4 files: FunctionsLib.h / cpp and QtSharedLib.h / cpp

    ========
    FunctionsLib.h

    @
    #ifndef FUNCTIONSLIB_H
    #define FUNCTIONSLIB_H
    #endif

    #include “QtSharedLib_global.h”
    QTSHAREDLIBSHARED_EXPORT int Add(int a, int b);
    @

    ========
    FunctionsLib.cpp

    @
    #include “FunctionsLib.h”

    QTSHAREDLIBSHARED_EXPORT int Add(int a, int b)
    { return (a+b);
    }
    @

    ========
    QtSharedLib.h

    @
    #ifndef QTSHAREDLIB_H
    #define QTSHAREDLIB_H
    #include “QtSharedLib_global.h”

    class QTSHAREDLIBSHARED_EXPORT QtSharedLib {
    public:
    QtSharedLib();
    int Add(int a, int b);
    };
    #endif // QTSHAREDLIB_H
    @

    ========
    QtSharedLib.cpp

    @
    #include “QtSharedLib.h”

    QtSharedLib::QtSharedLib()
    {
    }
    int QtSharedLib::Add(int a, int b)
    { return (a+b);
    }
    @

    It builds succefully.

    Then I create a Qt Widget application, add a button. I change the .pro file:

    @
    QT += core gui
    TARGET = Qt_WidgetApp
    TEMPLATE = app
    SOURCES += main.cpp MainWindow.cpp
    HEADERS += MainWindow.h
    FORMS += MainWindow.ui

    INCLUDEPATH += ../QtSharedLib
    LIBS += ./debug/QtSharedLib.lib
    LIBS += ./debug/QtSharedLib.dll
    @

    in the button click handling routine:

    @
    void MainWindow::on_pushButton_clicked()
    { int c = Add(1, 2); c = c+ 1;
    }
    @

    When I try to build the application, I got:

    @
    C:\Qt Test\Qt_WidgetApp-build-desktop\debug\QtSharedLib.dll:-1: error: LNK1107: invalid or corrupt file: cannot read at 0×2C8
    @

    What is the problem?

    [EDIT: code formatting, Volker]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      I don't know if that's the problem, but this line is not necessary:

      @
      LIBS += ./debug/QtSharedLib.dll
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

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

        Even I comment it out, I still can not build it successfully.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HuXiKa
          wrote on last edited by
          #4

          My first guess would be that if in your "whatever_global.h"
          @#if defined(TEST_LIBRARY)

          define whateverSHARED_EXPORT Q_DECL_EXPORT

          #else

          define whateverSHARED_EXPORT Q_DECL_IMPORT

          #endif@

          looks like this, so your whateverSHARED_EXPORT macro isn't good for you, use Q_DECL_EXPORT only. (If not, then it's OK.)

          The more important is, that you have to add to your .pro file the
          @LIBS -Ldebug -lQtSharedLib@
          instead of the other lines (if your dll is in your debug folder and your dll name is QtSharedLib.dll, if not, write the path of the folder which contains your dll file after -L and write the dll name without the .dll extension afterl -l).

          If you can find faults of spelling in the text above, you can keep them.

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

            In QtSharedLib_global.h

            @#if defined(QTSHAREDLIB_LIBRARY)

            define QTSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT

            #else

            define QTSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT

            #endif@

            By the way,

            LIBS -Ldebug -lQtSharedLib

            is not for win32, is that right? I try it and give error.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HuXiKa
              wrote on last edited by
              #6

              It is for win, my bad, what I wrote is wrong :)

              @LIBS += -Ldebug -lQtSharedLib @
              Don't forget the += symbol like I did.

              If you can find faults of spelling in the text above, you can keep them.

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

                I tried to use:

                @LIBS += -Ldebug -lQtSharedLib @

                But failed. The error:

                :-1: error: LNK1104: cannot open file 'lQtSharedLib.obj'

                But if I try to use:

                @LIBS += -Ldebug -QtSharedLib @

                It seems work. My question is - Why I need the obj file? What happens if I only have dll file?

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

                  Also, When I set a break point inside on_pushButton_clicked(), when I tried to step through it. I always got into Disassembler, not the c++ source code. Anyone know why?

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

                    The COFF file message is due to a compiler bug. See https://qt-project.org/wiki/Category:Tools::msvc

                    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