QT Creator中调用动态库文件例程
在linux中,qt里面已经集成了最新的开发平台QT Creator,下面是我练习调用动态库(.so文件)的例程:
1、打开QT Creator,点击File-》New...菜单,选择C++ Libarary工程,点击下一步,输入工程名称(本例为zsz)即可,这没什么可说的。
工程文件(.pro)程序清单
# -------------------------------------------------
# Project created by QtCreator 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, int j);
int add(int i, int j);
};
extern "C" MY_EXPORT int diff(int i, int j);
#endif // MYLIB_H
mylib.cpp文件程序清单:
#include "mylib.h"
extern "C" MY_EXPORT int mylib::mymax(int i,int j)
{
if(i>=j)
return i;
else
return j;
}
extern "C" MY_EXPORT int mylib::add(int i,int j)
{
return i+j;
}
extern "C" MY_EXPORT int diff(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 by QtCreator 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("Hello world!zwd");
hello.resize(400, 200);
mylib a;
qDebug()<<"In the two numbers 9 and 15,maxxer is "<< a.mymax(15,9);
qDebug()<<"Add the two numbers 9 and 15,result is "<< a.add(15,9);
qDebug()<<"Diff the two numbers 9 and 15,result is "<< diff(15,9);
hello.show();
return app.exec();
}
补充:软件开发 , C语言 ,