Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create and use shared library with qt

I am new to the shared library stuff, so I have question about how to create/use a shared library, I am using Qt Creator with qt 5.4.2 with Microsoft Visual C++ 11.0 Compliler.

In my project, I will need to create a dll which call functions from an external library (there are .h, .lib, .dll to use from). To understand how export/import of functions from library work, I tried to create a simple library with one function and use this in another programm first. After reading different tutorials, I managed to create the library. In Qt Creator, New Project->Library(C++ Library)->Type(shared library)Name: sharedlib->Modules(QtCore)->Finish.

sharedlib.h:

#ifndef SHAREDLIB_H
#define SHAREDLIB_H

#include <QtCore/qglobal.h>

#if defined(SHAREDLIB_LIBRARY)
#  define SHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

extern "C" SHAREDLIBSHARED_EXPORT int add(int a, int b);

#endif // SHAREDLIB_H

sharedlib.cpp:

#include "sharedlib.h"
#include <stdio.h>

extern "C" SHAREDLIBSHARED_EXPORT int add(int a, int b)
{
    return a + b;
}

only added a simple function to add 2 numbers.

after build, I get sharedlib.dll and sharedlib.lib and some other files, (no .a file like in some tutorials, I thought its because I am using microsoft vc compiler which give the .lib file instead).

Now to create a second programm in which I want to use the library: New Project->Qt Console Application->Name(loadlib)->Finish, then I copied the sharedlib.lib, sharedlib.h, sharedlib.dll into the loadlib directory. (do I need them all? and where shall I put them exactly?) According to tutorial, right-click on the project->add library->external library->choose the .lib file inside the loadlib directory, uncheck the Linux and Mac under Platform and choose the Dynamic Linkage. this is my loadlib.pro:

QT       += core
QT       -= gui

TARGET = loadlib
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lsharedlib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lsharedlib

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
  1. if I put .h and .dll/.lib file in subfolder like loadlib/include and loadlib/libs, it will change to INCLUDEPATH += $$PWD/include DEPENDPATH += $$PWD/include and LIBS += -L$$PWD/libs -lsharedlib, right?
  2. do I need to copy all 3 files to my loadlib directory?
  3. main.cpp:

    #include <QCoreApplication>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        // simple Debug output to add 7 and 3
    
        return a.exec();
    }
    

    how do I actually use the add function here?

EDIT: I changed few things, got rid of the sharedlib_global.h and paste the content into sharedlib.h, and get rid of the class,can I call a function directly without wrap this into a class?

like image 661
yangsunny Avatar asked Jan 25 '16 11:01

yangsunny


People also ask

Is Qt Creator an IDE?

Qt Creator is a cross-platform integrated development environment (IDE) built for the maximum developer experience. Qt Creator runs on Windows, Linux, and macOS desktop operating systems and allows developers to create software across desktop, mobile, and embedded platforms.

How to add external libraries in Qt Creator?

alternatively you can right-click your project in Qt Creator and select "Add Library...", choose "External library" and browse for your library file: For libraries compiled with MSCV compiler in windows, you look for .lib or .dll

How do I use qlibrary with shared libraries?

QLibrary can be used for loading shared libraries at runtime. In this case you only need access to the .dll, access to the headers and .lib file (s) is not necessary. The following example shows how to set up a library for usage with QLibrary.

How to create libraries depending on other libraries in qmake?

To create libraries depending on other libraries, you simply specify the QMake LIBSalso in you shared library project. If you fail to do so, you'll get runtime errors. Step 4: Set up runtime linker paths While it now builds, it needs to be able to find your shared libraries at runtime.

How many projects will I need to create in Qt Creator?

We'll need at least two projects in Qt Creator; one will be the shared library, the other one the main()and maybe some other classes. This, of course, depends on the model you're trying to put life to.


2 Answers

Everything you've done so far is correct. Now just include the library header file sharedlib.h in your main.cpp or whichever file and you can then use add() function there.

#include <QCoreApplication>
#include <QDebug>
#include "sharedlib.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // simple Debug output to add 7 and 3

    SharedLib lib;
    int a = 5, b = 6;
    int sum = lib.add (a, b);

    return a.exec();
}

You need to pack sharedlib.dll in the same directory along with the executable file when deploying.

like image 183
ninja Avatar answered Sep 23 '22 07:09

ninja


Try this (main.cpp):

#include "sharedlib.h"

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // simple Debug output to add 7 and 3
    SharedLib sLib;
    qDebug() << sLib.add(7, 3); // should print 10

    return 0;   // just exit

//    return a.exec();  // you need to kill / force stop your app if you do ths.
}

If you can compile the above, then your library is working as expected.

like image 24
ramtheconqueror Avatar answered Sep 26 '22 07:09

ramtheconqueror