A
A
Andrey PINEAPPLE2016-07-08 18:21:57
Qt
Andrey PINEAPPLE, 2016-07-08 18:21:57

How to include your DLL in QT?

There is a compiled library in which there is one function:
dynlib.h:

#include <QString>

extern "C++" {
    QString oddUpper(const QString& str);
}

dynlib.cpp:
#include "dynlib.h"

QString oddUpper(const QString& str)
{ 
    QString strTemp;

    for (int i = 0; i < str.length(); ++i) {
        strTemp += (i % 2) ? str.at(i) : str.at(i).toUpper();
    }

    return strTemp;
}

.pro:
TEMPLATE = lib
DESTDIR  = ..
QT       -= gui
SOURCES  = dynlib.cpp
HEADERS  = dynlib.h
TARGET   = dynlib

And there is a program that should use this function:
app.cpp:
#include <QtWidgets>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QLabel       lbl("this is the example text");
    QLibrary     lib("dynlib");

    typedef QString (*Fct) (const QString&);
    Fct fct = (Fct)(lib.resolve("oddUpper"));
    if (fct) {
        lbl.setText(fct(lbl.text()));
    }
    else{
        lbl.setText("DLL NOT FOUND!");
    }
    lbl.show();

    return app.exec();
}

app.pro:
TEMPLATE = app
QT          += widgets
DESTDIR  = ..
SOURCES	 = main.cpp
TARGET	 = MyApplication

The executable file of the program is located in the same place as the lib, but the program itself does not see the library.
b6937bcdb1e44e4eaefc2b240378401c.png
Help me to understand. I understand that I still need to register something in the PRO file of my application ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-07-08
@AndreyHudz90

1. Instead of extern "C++" you need extern "C" , otherwise the name of your function will be spoofed, and it will be very difficult to find it by name.
2. Get Dependency Walker and check that your function is actually exporting correctly from the DLL.
3. So what exactly is the problem? What error are you getting?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question