Qt&VS-How to create a dynamic library with interface

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 VSa little cut to the heart, hereby record.

The creation of dynamic library in Qt

QtCreatorYou can create a dynamic library through the wizard, which is amazing.

Insert picture description here
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 classmodifier

  • 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.

Insert picture description here
Create a new App; if you are writing an interface class, the general base class is selectedQWidget

Insert picture description here

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_EXPORTmodifications toclass

It is noteworthy that __DLL_LIBRARYthe 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 Qtthe middle can be pro文件added:

DEFINES += __DLL_LIBRARY

In VSthe add in here:

Insert picture description here

test program

After compilation needs to be *.h, *.dll, *.libpackaged together.

QTThe import library file wizard can be used in the test program: ·
Insert picture description here
and create one in the UI interface QWidget:

Insert picture description here
Call by way of promotion:

Insert picture description here

If in VSthe attributes you need to configure it:

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/108719195
Recommended