您的位置:首页 > 编程语言 > Qt开发

Qt dll的导出与调用

2013-09-13 13:10 363 查看
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: