[Qt]动态库--window

来源:互联网 发布:焊接机器人的软件功能 编辑:程序博客网 时间:2024/05/16 05:20
在linux中,qt里面已经集成了最新的开发平台QT Creator,下面是我练习调用动态库(.so文件)的例程:
1、打开QT Creator,点击File-》New...菜单,选择C++Libarary工程,点击下一步,输入工程名称(本例为zsz)即可,这没什么可说的。
    工程文件(.pro)程序清单

#-------------------------------------------------

# Project created byQtCreator 2009-03-02T10:09:35

#-------------------------------------------------

TARGET = zsz

TEMPLATE = lib

CONFIG += plugin

DEPENDPATH += .

INCLUDEPATH += .

 

SOURCES += mylib.cpp

HEADERS += mylib.h


mylib.h文件程序清单:



#ifndef MYLIB_H

#define MYLIB_H

 

#ifdef Q_WS_WIN//表示在windows环境

#define MY_EXPORT__declspec(dllexport)

#else

#define MY_EXPORT

#endif

 

class mylib {

public:

int mymax(int i, intj);

int add(int i, int j);

};

 

extern "C" MY_EXPORT intdiff(int i, int j);

 

#endif // MYLIB_H


mylib.cpp文件程序清单:



#include "mylib.h"

 

 

extern "C" MY_EXPORT intmylib::mymax(int i,int j)

{

if(i>=j)

return i;

else

return j;

}

 

extern "C" MY_EXPORT intmylib::add(int i,int j)

{

return i+j;

}

 

extern "C" MY_EXPORT intdiff(int i, int j)

{

if(i>=j)

return i-j;

else

return j-i;

}

对该工程进行编译,生成libzsz.so文件。


2、创建一个GUI工程,并把zsz工程下的mylib.h和libzsz.so两个文件拷贝到当前目录下。

工程文件程序清单:



#-------------------------------------------------

# Project created byQtCreator 2009-03-02T10:15:03

#-------------------------------------------------

TARGET =

TEMPLATE = app

 

QT += svg

DEPENDPATH += .

INCLUDEPATH += .

LIBS += ./libzsz.so

 

SOURCES += main.cpp

HEADERS += mylib.h


main.cpp中的程序为:



#include<qapplication.h>

#include<QPushButton>

#include<QLibrary>

#include<QtDebug>

#include "mylib.h"

 

int main(int argc, char*argv[])

{

QApplication app(argc,argv);

QPushButton hello("Helloworld!zwd");

hello.resize(400,200);

mylib a;

 

qDebug()<<"In the two numbers 9 and15,maxxer is "<< a.mymax(15,9);

qDebug()<<"Add the two numbers 9 and15,result is "<< a.add(15,9);

qDebug()<<"Diff the two numbers 9 and15,result is "<< diff(15,9);

 

hello.show();

return app.exec();

}

原创粉丝点击