How to link to a dll

From Qt Wiki
Jump to: navigation, search


introduction

This is a guide to use a 3rd party dll file. First a simple .dll file is created and used, next a 3rd party dll is used. This tutorial assumes Windows7 SP1 and Qt5.5.1 32 bit

generate mylibrary.dll

Start Qt Designer. Choose File->New File of project->Library->C++ Library->Choose->Type: Shared Library, Name: mylibrary->Next->Next->Select Required Modules->QtCore->Next->Class Name: Mylibrary, Header file: mylibrary.h, Source file: mylibrary.cpp->Next->Finish.

add method calc in mylibrary

edit mylibrary.h as follows:

#ifndef MYLIBRARY_H
#define MYLIBRARY_H

#include "mylibrary_global.h"

class MYLIBRARYSHARED_EXPORT Mylibrary
{ 

public:
    Mylibrary();
    int calc();
};

#endif // MYLIBRARY_H

edit mylibrary.cpp as follows:

#include "mylibrary.h"


Mylibrary::Mylibrary()
{
}

int Mylibrary::calc()
{
    return 5;
}


build mylibrary.dll

Choose build->build all. In the build directory\debug\ 3 new files are made: libmylibrary.a mylibrary.dll and mylibrary.o Then close the project, and close all open files.

use mylibrary.dll

Start Qt Designer. Choose File->New File of project->Application->Qt Widgets Application->choose->Name: myProgram->next->next->Class name: MainWindow, BaseClass: QMainWindows, Headerfile: mainwindow.h, source file: mainwindow.cpp form: mainwindow.ui, next->finish

copy .lib to project directory

in the directory with myProgram.pro, create the folder myLibrary. Enter this directory, and then copy the following files to it: mylibrary.dll, mylibrary.h and mylibrary_global.h

link to myLibrary.dll

edit myProgram.pro as follows:

#-------------------------------------------------
#
# Project created by QtCreator 2016-07-28T12:54:41
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myProgram
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

#compiles in Windows7, but is OS dependant:
#INCLUDEPATH += "./myLibrary"

#correct method:
INCLUDEPATH += "$$PWD/myLibrary"

#compiles, but hardcoded path
#g++ -Wl,-subsystem,windows -mthreads -o debug\myProgram.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o  -lmingw32 -LC:/Qt/Qt5.5/5.5/mingw492_32/lib -lqtmaind -lshell32 c:/somewhere/myProgram/myLibrary/mylibrary.dll -lQt5Widgetsd -lQt5Guid -lQt5Cored
#LIBS += "c:/somewhere/myProgram/myLibrary/mylibrary.dll"

#incorrect
#g++ -Wl,-subsystem,windows -mthreads -o debug\myProgram.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o  -lmingw32 -LC:/Qt/Qt5.5/5.5/mingw492_32/lib -lqtmaind -lshell32 ./myLibrary/mylibrary.dll -lQt5Widgetsd -lQt5Guid -lQt5Cored
#g++: error: ./myLibrary/mylibrary.dll: No such file or directory
#LIBS += "./myLibrary/mylibrary.dll"

#compiles, with relative path, correct method
#g++ -Wl,-subsystem,windows -mthreads -o debug\myProgram.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o  -lmingw32 -LC:/Qt/Qt5.5/5.5/mingw492_32/lib -lqtmaind -lshell32 C:/somewhere/myProgram/myLibrary/mylibrary.dll -lQt5Widgetsd -lQt5Guid -lQt5Cored
LIBS += "$$PWD/myLibrary/mylibrary.dll"

edit mainwindow.cpp as follows:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include "myLibrary/mylibrary.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Mylibrary myLib;
    qDebug() << myLib.calc();
}

MainWindow::~MainWindow()
{
    delete ui;
}

build myProgam

Choose Build->rebuild all. Copy mylibrary.dll to the build directory, next to myProgram.exe Choose Debug->Start Debugging->Start Debugging After a few moments, application output shows

Debugging starts
5

use 3rd party dll from microchip

Create a new Qt project to use the dll

Start Qt Designer. Choose File->New File of project->Application->Qt Widgets Application->choose->Name: mcp2221client->next->next->Class name: MainWindow, BaseClass: QMainWindows, Headerfile: mainwindow.h, source file: mainwindow.cpp form: mainwindow.ui, next->finish

Download mcp2221 dll

download mcp2221 dll from microchip. Create the folder mcp2221_DLL next to the .pro file. Extract the zip file, then locate and copy the following files to the mcp2221_DLL folder:

mcp2221_dll_um_x86.dll
mcp2221_dll_um.h

link to mcp2221_dll_um_x86.dll

edit mcp2221client.pro as follows:

edit mainwindow.cpp as follows:

external information used

The source of the above information Stackoverflow: Qt and Linking to an external DLL