Call the dynamic library file routine in QT Creator

Source: Internet
Author: User

 

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 ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.