How to publish a pure C + + project as a DLL in QT (Hyper-detailed steps)

Source: Internet
Author: User

Directory

    • General Creation Method
    • Methods & Calling methods for exporting common functions
    • Methods & Calling methods for exporting classes and their member functions

As we all know, we can export the classes and functions in a C + + project to form a. dll file for use by other programs, and the following describes how to use it in a QT environment.

First create a shared library, as follows:

The next step will be the class dialog box and so on, do not care about it, directly click Create, and then delete the class later.

Once created, you will find a header file containing global, which defines __declspec (dllexport), __declspec (dllimport), and so on, which can be deleted (if you want to do this, delete it).

There is an important next step, in the. Pro file, add

Config + = dll   Even if your code is written in CONFIG + = Staticlib, change it.

Let's start with our concrete approach to creation.

There are two modes that follow the operation of the exported DLL:

    • Export Normal method (can be called statically after export, or can be called dynamically)

First, the contents of the header file:

header file: When a method declaration, preceded by __declspec (dllimport), can also be defined by # define, see the code://In order to use a common header file in a program that is used in the future, __declspec (dllexport) is used to create the DLL, and __declspec (dllimport) is used when using the DLL, so you can write each one individually .#defineTestdllshared_export __declspec (dllexport)#ifdef Testdllshared_export#else#defineTestdllshared_export __declspec (dllimport)#endif//I'm going to define 4 common functions below:Testdllshared_exportinttest1 (); Testdllshared_exportintTest2 (void); Testdllshared_exportintTest3 (inta); Testdllshared_exportintTest4 (intAintb);

Then in the corresponding CPP source files implemented Test1, Test2, Test3, test4 These methods (for the sake of simplicity, I only output a word):

. cpp source files://Remember to add the header file above//Remember to add iostream header file, or cout can't useinttest1 () {std::cout<<"test1"<<Std::endl;}intTest2 (void) {Std::cout<<"test2"<<Std::endl;}intTest3 (inta) {Std::cout<<"test3"<<Std::endl;}intTest4 (intAintb) {Std::cout<<"test4"<<Std::endl;}

Next you can create a successful creation (after creating the failure please check carefully, the code has been verified), you will find in your project output directory a. dll file that is consistent with the project name.

Ok! Next we will use this DLL:

There are two calling methods when using a DLL, one for static calls and one for dynamic invocation.

    • Static call

First, create a regular C + + project and copy the above generated. dll into your project output directory (that is, with the. exe file);

Next, open the. Pro file in your regular C + + project and add the detailed. dll file address in the following format

LIBS + = project Output path \dll file name

As mine is.

LIBS + = d:\desktop\go\c++learnprogram\qt\build-test_dll-qt-release\release\hptickdll.dlld:\desktop\go\c ++learnprogram\qt\build-test_dll-qt-release\release is my project output path, HpTickDll.dll is my DLL file, use it to change a bit.

Next, copy the header file when you create the. dll file to the current project path and add it to the project, note: If you do not follow my definition of # define criteria above, please write __declspec (dllimport) again.

Next, in the. cpp file contains the header file, you can use the previous functions, such as directly with Test1 () and so on, and usually written the same.

    • Dynamic invocation

!!! Special Note: We did not use the extern "C" when we created it, nor did it use the. def file to keep the function name constant (tried many times and would not use a. def file, would welcome the message), so the dynamic invocation of the function name to change! Because the compiler has changed the function name!

So, you're going to ask us how do we know what a function name in a DLL turns into? Do not worry, the Internet directly search ". dll viewer", everywhere, download down, select our just the. dll file can be seen, the following are our functions test1, test2, Test3, test4 the new name (you may be different from mine OH):

Notice the section in the Red box (no other, there's something else in my file), This is the name of the four function in the. dll file, I guess a little bit, the following V for the argument is the void type, the number of I represents the number of int type parameter, the preceding letter is not very clear (note: The number of parameters is not listed correctly, but we only need the correct function name, not affect).

ok! This is good! Look at the steps:

The same is the creation of a regular C + + project, the difference is that the. Pro file does not add the "LIBS + = project Output path \dll file name" sentence.

Next write the main code in the. cpp file (without adding the previous header file):

#include <iostream>#include<windows.h>intMain () {//function pointers are defined first to receive functions with different parameterstypedefint(CALLBACK *FUCV)    (); typedefint(CALLBACK *fuci) (int); typedefint(CALLBACK *fucii) (int,int); //Get a handle to a. DLL file that requires the support of the header file Windows.hHInstance Hdll=loadlibrary (L"TestDll.dll");//L refers to the width of the string, if not write L, there will be errors, details please self- examination//Note that the function name in the. dll file is used hereFUCV t1= (FUCV) GetProcAddress (hDLL,"_z5test1v"); FUCV T2= (FUCV) GetProcAddress (hDLL,"_z5test2v"); FUCI T3= (fUCI) GetProcAddress (hDLL,"_z5test3i"); fucii T4= (FUCII) GetProcAddress (hDLL,"_z5test4ii"); //Now the T1 performs the function of the test1, and so onT1 ();    T2 (); T3 (1); T4 (1,2);    FreeLibrary (hDLL); return 0;}

The above is the common function of creating. dll and using the. dll method, it seems that dynamic call is not very troublesome? But it has many advantages (self-examination), and it is said that the function name here can take advantage of the. def file implementation does not change the name, save a lot of trouble, but I tried various methods, such as in. Pro using Def_file to add. def files or not 55555, will be able to leave a message Oh!

    • exports a class and its member functions , which can be called statically after export. Temporarily does not invoke dynamically, some books say the class does not support dynamic invocation, there is said in the class to write a method to return the class object, but personally think this is wrong, because it must be defined by their own function pointer to define the return class object method, it is not possible to successfully "verified", of course, there may be a better way, Have to know the Welcome exchange ha)

For a class, when created: at the back of class, a defined __declspec (dllexport) is added to the name, and a __declspec (dllimport) is used to refer to the previous # define conditional definition method. Example:

#defineHptickdllshared_export __declspec (dllexport)#ifdef Hptickdllshared_export#else#defineHptickdllshared_export __declspec (dllimport)#endifclassHptickdllshared_export Hptickdll//I've defined the class Hptickdll here.{    Public:    intStart ();//Note that member functions are not hptickdllshared_export before they are added    intGetTime ();Private: Large_integer Li;    Longlong start, end, freq; intusetime;};

Statically called methods like normal functions, you can directly use the class and its member functions (don't forget to add a header file), not to elaborate.

---restore content ends---

Directory

    • General Creation Method
    • Methods & Calling methods for exporting common functions
    • Methods & Calling methods for exporting classes and their member functions

As we all know, we can export the classes and functions in a C + + project to form a. dll file for use by other programs, and the following describes how to use it in a QT environment.

First create a shared library, as follows:

The next step will be the class dialog box and so on, do not care about it, directly click Create, and then delete the class later.

Once created, you will find a header file containing global, which defines __declspec (dllexport), __declspec (dllimport), and so on, which can be deleted (if you want to do this, delete it).

There is an important next step, in the. Pro file, add

Config + = dll  // Even if your code is written in config + = staticlib, change it .

Let's start with our concrete approach to creation.

There are two modes that follow the operation of the exported DLL:

    • Export Normal method (can be called statically after export, or can be called dynamically)

First, the contents of the header file:

header file: When a method declaration, preceded by __declspec (dllimport), can also be defined by # define, see the code://In order to use a common header file in a program that is used in the future, __declspec (dllexport) is used to create the DLL, and __declspec (dllimport) is used when using the DLL, so you can write each one individually .#defineTestdllshared_export __declspec (dllexport)#ifdef Testdllshared_export#else#defineTestdllshared_export __declspec (dllimport)#endif//I'm going to define 4 common functions below:Testdllshared_exportinttest1 (); Testdllshared_exportintTest2 (void); Testdllshared_exportintTest3 (inta); Testdllshared_exportintTest4 (intAintb);

Then in the corresponding CPP source files implemented Test1, Test2, Test3, test4 These methods (for the sake of simplicity, I only output a word):

. cpp source files://Remember to add the header file above//Remember to add iostream header file, or cout can't useinttest1 () {std::cout<<"test1"<<Std::endl;}intTest2 (void) {Std::cout<<"test2"<<Std::endl;}intTest3 (inta) {Std::cout<<"test3"<<Std::endl;}intTest4 (intAintb) {Std::cout<<"test4"<<Std::endl;}

Next you can create a successful creation (after creating the failure please check carefully, the code has been verified), you will find in your project output directory a. dll file that is consistent with the project name.

Ok! Next we will use this DLL:

There are two calling methods when using a DLL, one for static calls and one for dynamic invocation.

    • Static call

First, create a regular C + + project and copy the above generated. dll into your project output directory (that is, with the. exe file);

Next, open the. Pro file in your regular C + + project and add the detailed. dll file address in the following format

LIBS + = project Output path \dll file name

As mine is.

LIBS + = d:\desktop\go\c++learnprogram\qt\build-test_dll-qt-release\release\hptickdll.dlld:\desktop\go\c ++learnprogram\qt\build-test_dll-qt-release\release is my project output path, HpTickDll.dll is my DLL file, use it to change a bit.

Next, copy the header file when you create the. dll file to the current project path and add it to the project, note: If you do not follow my definition of # define criteria above, please write __declspec (dllimport) again.

Next, in the. cpp file contains the header file, you can use the previous functions, such as directly with Test1 () and so on, and usually written the same.

    • Dynamic invocation

!!! Special Note: We did not use the extern "C" when we created it, nor did it use the. def file to keep the function name constant (tried many times and would not use a. def file, would welcome the message), so the dynamic invocation of the function name to change! Because the compiler has changed the function name!

So, you're going to ask us how do we know what a function name in a DLL turns into? Do not worry, the Internet directly search ". dll viewer", everywhere, download down, select our just the. dll file can be seen, the following are our functions test1, test2, Test3, test4 the new name (you may be different from mine OH):

Notice the section in the Red box (no other, there's something else in my file), This is the name of the four function in the. dll file, I guess a little bit, the following V for the argument is the void type, the number of I represents the number of int type parameter, the preceding letter is not very clear (note: The number of parameters is not listed correctly, but we only need the correct function name, not affect).

ok! This is good! Look at the steps:

The same is the creation of a regular C + + project, the difference is that the. Pro file does not add the "LIBS + = project Output path \dll file name" sentence.

Next write the main code in the. cpp file (without adding the previous header file):

#include <iostream>#include<windows.h>intMain () {//function pointers are defined first to receive functions with different parameterstypedefint(CALLBACK *FUCV)    (); typedefint(CALLBACK *fuci) (int); typedefint(CALLBACK *fucii) (int,int); //Get a handle to a. DLL file that requires the support of the header file Windows.hHInstance Hdll=loadlibrary (L"TestDll.dll");//L refers to the width of the string, if not write L, there will be errors, details please self- examination//Note that the function name in the. dll file is used hereFUCV t1= (FUCV) GetProcAddress (hDLL,"_z5test1v"); FUCV T2= (FUCV) GetProcAddress (hDLL,"_z5test2v"); FUCI T3= (fUCI) GetProcAddress (hDLL,"_z5test3i"); fucii T4= (FUCII) GetProcAddress (hDLL,"_z5test4ii"); //Now the T1 performs the function of the test1, and so onT1 ();    T2 (); T3 (1); T4 (1,2);    FreeLibrary (hDLL); return 0;}

The above is the common function of creating. dll and using the. dll method, it seems that dynamic call is not very troublesome? But it has many advantages (self-examination), and it is said that the function name here can take advantage of the. def file implementation does not change the name, save a lot of trouble, but I tried various methods, such as in. Pro using Def_file to add. def files or not 55555, will be able to leave a message Oh!

    • exports a class and its member functions , which can be called statically after export. Temporarily does not invoke dynamically, some books say the class does not support dynamic invocation, there is said in the class to write a method to return the class object, but personally think this is wrong, because it must be defined by their own function pointer to define the return class object method, it is not possible to successfully "verified", of course, there may be a better way, Have to know the Welcome exchange ha)

For a class, when created: at the back of class, a defined __declspec (dllexport) is added to the name, and a __declspec (dllimport) is used to refer to the previous # define conditional definition method. Example:

#defineHptickdllshared_export __declspec (dllexport)#ifdef Hptickdllshared_export#else#defineHptickdllshared_export __declspec (dllimport)#endifclassHptickdllshared_export Hptickdll//I've defined the class Hptickdll here.{    Public:    intStart ();//Note that member functions are not hptickdllshared_export before they are added    intGetTime ();Private: Large_integer Li;    Longlong start, end, freq; intusetime;};

Statically called methods like normal functions, you can directly use the class and its member functions (don't forget to add a header file), not to elaborate.

Methods for publishing a pure C + + project as a DLL in QT (Hyper-detailed steps)

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.