Article Directory
Preface
In the recently contacted projects, after the interface is divided, the dynamic library is used to deliver to colleagues for integration and call. Development environment Qt+VS
, just getting started with VS
a little cut to the heart, hereby record.
The creation of dynamic library in Qt
QtCreator
You can create a dynamic library through the wizard, which is amazing.
In the present example testdll
, it is worth noting that:
pro
//...
TEMPLATE = lib
DEFINES += TESTDLL_LIBRARY
testdll_global.h
#ifndef TESTDLL_GLOBAL_H
#define TESTDLL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(TESTDLL_LIBRARY)
# define TESTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define TESTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // TESTDLL_GLOBAL_H
testdll.h
#ifndef TESTDLL_H
#define TESTDLL_H
#include "testdll_global.h"
class TESTDLLSHARED_EXPORT Testdll
{
public:
Testdll();
};
#endif // TESTDLL_H
It can be found mainly through the macro definitions to control the header file class
modifier
- Q_DECL_EXPORT
That__declspec(dllexport)
indicates导出dll
- Q_DECL_IMPORT
That__declspec(dllimport)
indicates导入dll
Qt interface class
It is very easy to use the wizard.
Create a new App; if you are writing an interface class, the general base class is selectedQWidget
Dynamic library with interface
We only need to create the interface class and add the macro definition in the header file
#if defined(__DLL_LIBRARY)
# define xxx_EXPORT __declspec(dllexport)
#else
# define xxx_EXPORT __declspec(dllimport)
#endif
And xxx_EXPORT
modifications toclass
It is noteworthy that __DLL_LIBRARY
the best in the project configuration is added in, avoid forget to change results in a compile error when packaging: Do not allow the definition of dllimport static data members.
In Qt
the middle can be pro文件
added:
DEFINES += __DLL_LIBRARY
In VS
the add in here:
test program
After compilation needs to be *.h
, *.dll
, *.lib
packaged together.
QT
The import library file wizard can be used in the test program: ·
and create one in the UI interface QWidget
:
Call by way of promotion:
If in VS
the attributes you need to configure it: