1、打开QT Creator,点击File-》New...菜单,选择C++Libarary工程,点击下一步,输入工程名称(本例为zsz)即可,这没什么可说的。
#-------------------------------------------------
# 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();
}