In linux, qt has integrated the latest development platform QT Creator. Below is a routine for me to practice calling a dynamic library (. so file:
1. Open QT Creator and click File-New... select the C ++ Libarary project, click Next, and enter the project name (zsz in this example.
Project file (. pro) program list
#-------------------------------------------------
# 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 file program list:
# Ifndef MYLIB_H
# Define MYLIB_H
# Ifdef Q_WS_WIN // indicates the windows Environment
# 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
List of mylib. cpp file programs:
# 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;
}
Compile the project to generate the libzsz. so file.
2. Create a GUI project and copy the mylib. h and libzsz. so files under the zsz project to the current directory.
Project File program list:
#-------------------------------------------------
# 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
The program in main. cpp is:
# 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;
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.exe c ();
}