问题
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/
- 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/includeandLIBS += -L$$PWD/libs -lsharedlib, right? - do I need to copy all 3 files to my loadlib directory?
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/34991859/create-and-use-shared-library-with-qt