Qt dll的导出与调用

来源:互联网 发布:原盘播放软件 编辑:程序博客网 时间:2024/04/28 17:00

Qt中动态链接库(dll)的导出

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 <QDebug>
#include "testdll_global.h"
class TESTDLLSHARED_EXPORT Testdll
{
    
public:
    Testdll();
public:
    static void add( int a1, int a2 );
    static void sub( int s1, int s2 );
};
extern "c" TESTDLLSHARED_EXPORT void mul( int m1, int m2 );
#endif // TESTDLL_H

testdll.cpp

#include "testdll.h"Testdll::Testdll(){}void Testdll::add(int a1, int a2){    qDebug()<<"a1+a2="<<a1+a2;}void Testdll::sub(int s1, int s2){    qDebug()<<"s1-s2="<<s1-s2;}void mul(int m1, int m2){    qDebug()<<"m1*m2="<<m1*m2;}

编译后生成 lib文件和dll文件

dll文件的隐式调用

1 需要在调用程序中引入 testdll.h文件

2 需要导入testdll.lib文件 #pragma comment(lib, "testdll.lib")

        

#include <QCoreApplication>#include "./lib/testdll_global.h"#include "./lib/testdll.h"#include <QLibrary>#pragma comment(lib, "./lib/testdll.lib")TESTDLLSHARED_EXPORT void mul( int, int);int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    Testdll::add( 3, 5 );    Testdll::sub( 5, 2 );    mul( 2, 7);    return a.exec();}