QT Export C + + class and qt static loading dll__c++

Source: Internet
Author: User
Tags export class win32

This article describes how to use Qcreator to create a new C + + Export Class (DLL) that contains QT libraries and how to call a C + + class in a DLL in Qt engineering. The steps of the whole experiment are as follows:


I. Host project (CALLER)

1, open Qcreator, create a new QT GUI project named "Host" (all defaults except name).

2, build and run host project, produce "MainWindow" window.

3, confirm the test is normal, close the window, and close the host project.


Second, the Library project

1, using Qcreator to create a new C + + library, called "Library".



2, select the desired QT library module.



Note: As shown above, you can select modules such as GUI and widgets.


3, add test information.

I added a line of print information to the constructor of the export class "Library" as follows:

#include "Library.h"


library::library ()
{
    Qdebug ("Create class ' library ' successfully!");
}


4, the exported headers and "*_global.h" files are copied to the host engineering directory and included in the search path of the host.

In fact, we can merge "*_global.h" into the export header file, which has less content and has only one import export declaration.

#ifndef library_global_h
#define LIBRARY_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined ( library_library)
#  define Libraryshared_export q_decl_export
#else
#  define Libraryshared_ EXPORT q_decl_import
#endif

#endif//Library_global_h

Note: the "LIBRARRY_LIBRARY" macro is defined in the Library.pro file.


5, build the library project, will generate *.lib,*.dll,*.a three output files.


Third, load test

1, copy the exported header file to the host Engineering directory and add the path to the Host.pro file.

2, right key to the project directory window of the project directory tree root node, select "Add Library."

Select External Library, and then add the library's path.



By completing this step, you can add an external library to your project. In fact, it adds the following code to the Host.pro:

Win32:config (Release, debug|release): LIBS =-l$ $PWD/. /build-library-desktop_qt_5_4_1_msvc2013_32bit-debug/release/-llibrary
Else:win32:CONFIG (Debug, debug| Release): LIBS + =-l$ $PWD/. /build-library-desktop_qt_5_4_1_msvc2013_32bit-debug/debug/-llibrary
Else:unix:LIBS + =-l$ $PWD/. /build-library-desktop_qt_5_4_1_msvc2013_32bit-debug/-llibrary

Includepath + = $ $PWD/. /build-library-desktop_qt_5_4_1_msvc2013_32bit-debug/debug
Dependpath + = $ $PWD/. /build-library-desktop_qt_5_4_1_msvc2013_32bit-debug/debug

3, add test code

Define the class "Library" in the MainWindow constructor.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "library.h"

Mainwindow::mainwindow ( Qwidget *parent):
    Qmainwindow (Parent),
    UI (new Ui::mainwindow)
{
    ui->setupui (this);
    Library A;
}

Mainwindow::~mainwindow ()
{
    delete ui;
}

4,debug, you will see the previously added print information in the console


Iv. Supplementary

1,QT can also use the Qlibrary class and its load function to dynamically load external libraries.

2, the experiment uses the MinGW compiler.

3, the use of VC + + compiler, when loading the library, there are compile errors. However, if you use the VC + + compiler, you can load the external library directly in vs.

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.