In Qt, the pure C ++ project is released as a dll method (detailed steps). qt detailed steps

Source: Internet
Author: User
Tags export class

In Qt, the pure C ++ project is released as a dll method (detailed steps). qt detailed steps

Directory

  • General Creation Method
  • Export methods and call methods of common functions
  • Methods and call methods for exporting classes and their member functions

 

As we all know, we canClass and FunctionExport to form a. dll file for use by other programs. The following describes how to use it in the Qt environment.

To create a shared library, follow these steps:

In the next step, a class dialog box will appear. You don't need to worry about it. Just click Create and delete all the classes later.

After the creation, you will find a header file containing global, which defines _ declspec (dllexport), _ declspec (dllimport), and so on, you can also delete it (if you want to follow the following method, delete it ).

In the. pro file, add

CONFIG + = dll // even if your code is written as CONFIG + = staticlib, you need to change it.

Next, let's start creating a method!

 

Based on the dll export operation, there are two modes:

  • Export common methods(Static or dynamic call can be performed after export)

First, the header file content:

 

Header file: Add _ declspec (dllimport) in front of the method declaration. You can also use # define to define it. See the code: // to share a header file with future programs, when creating a dll, _ declspec (dllexport) is used. When using a dll, _ declspec (dllimport) is used. You can write down # define TESTDLLSHARED_EXPORT _ declspec (dllexport) separately) # ifdef shard # else # define TESTDLLSHARED_EXPORT _ declspec (dllimport) # endif // below I want to define four common functions: struct int test1 (); TESTDLLSHARED_EXPORT int test2 (void ); TESTDLLSHARED_EXPORT int test3 (int a); TESTDLLSHARED_EXPORT int test4 (int a, int B );

Then implement the methods test1, test2, test3, and test4 in the corresponding cpp source file (for simplicity, I only output one sentence ):

. Cpp source file: // remember to add the header file above // remember to add the iostream header file. Otherwise, cout cannot use int test1 () {std: cout <"test1" <std :: endl;} int test2 (void) {std: cout <"test2" <std: endl;} int test3 (int a) {std :: cout <"test3" <std: endl;} int test4 (int a, int B) {std: cout <"test4" <std :: endl ;}

Next, you can create it. After the creation is successful (the creation fails, check carefully, and the code has been verified), you will find a name consistent with the project name in your project output directory. dll file.

OK! Next we will use this dll:

There are two methods to call a dll: static call and dynamic call.

  • Static call

First, create a regular CIDR project, and copy the generated .dllto your project output directory (together with the .exe file );

Next, open the. pro file in the General C ++ project and add the detailed. dll file address. The format is as follows:

 

LIBS + = full name of the project output path \ dll file

For example, 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. You can change it when using it.

Next, we will create. copy the header file of the dll file to the current project path and add it to the project. Note: if you do not follow the # define condition definition method above, please rewrite _ declspec (dllimport ).

Next, include this header file in the. cpp file, and you can use the previous functions as much as you like, such as using test1 () directly, and so on.

  • Dynamic call

!!! Note: extern "C" or extern "C" is not used during creation. def file keeps the function name unchanged (it won't be used after many attempts. def file, will be welcome to leave a message), so the function name needs to be changed during dynamic calling! Because the compiler has changed the function name!

So, you will surely ask how we know what the function name in the dll has become? Don't worry, search for ". dll viewer ", which is everywhere. After downloading it, select the one we just downloaded. the dll file can be viewed. Below are the new names of our functions test1, test2, test3, and test4 (You may be different from mine ):

Please pay attention to the section in the red box (don't worry about anything else, there are other things in my file). These are the four functions in. dll file name, I guess a little bit, the following v represents the parameter is void type, the number of I represents the number of int type parameters, the first letter is not clear (note: the number of parameters is not correctly listed, but we only need the correct function name, without affecting ).

OK! This is easy! Steps:

Create a common C ++ project. The difference is that the "LIBS + = project output path \ dll file full name" statement is not added to the. pro file.

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

# Include <iostream> # include <windows. h> int main () {// first define the function pointer to receive the typedef int (CALLBACK * Fucv) () and typedef int (CALLBACK * Fuci) functions with different parameters) (int); typedef int (CALLBACK * Fucii) (int, int); // get. dll file handle. The header file is required for windows. HINSTANCE hdll = LoadLibrary (L "testDll. dll "); // L indicates a wide string. If L is not specified, an error occurs. For details, check the string. // be sure to use it here. function name in the dll file Fucv t1 = (Fucv) GetProcAddress (hdll, "_ Z5test1v"); Fucv t2 = (Fucv) GetProcAddress (hdll, "_ Z5test2v "); fuci t3 = (Fuci) GetProcAddress (hdll, "_ Z5test3i"); Fucii t4 = (Fucii) GetProcAddress (hdll, "_ Z5test4ii "); // now t1 executes the test1 function, and so on t1 (); t2 (); t3 (1); t4 (1, 2); FreeLibrary (hdll); return 0 ;}

The above is the method for creating. dll and using. dll in common functions. Does dynamic calling seem very troublesome? But it has many advantages (please check it yourself), and it is said that the function name here can be used. def file implementation does not change the name, saving a lot of trouble, but I tried a variety of methods, such as in. add in DEF_FILE in pro. def file is still not 55555. You can leave a message!

  • Export class and its member functions(It can be called statically after export. Currently, it is not dynamically called. In some books, classes do not support dynamic calling. on the Internet, it is said to write a method in the class to return class objects. However, I personally think this is wrong, because at this time, the function pointer defined by myself must be used to define the method of the returned class object, and it is impossible to succeed [verified]. Of course, there may be better methods. If you know this, please feel free to contact us)

For a class, add the defined _ declspec (dllexport) before the class name after the class; replace it with _ declspec (dllimport ), you can also refer to the # define condition definition method above. Example:

# Define export _ declspec (dllexport) # ifdef export # else # define HPTICKDLLSHARED_EXPORT _ declspec (dllimport) # endifclass export HpTickDll // I have defined the class HpTickDll {public: int Start (); // note that you do not need to add HPTICKDLLSHARED_EXPORT int GetTime (); private: LARGE_INTEGER li; LONGLONG start, end, freq; int useTime;} before the member function ;};

Static call methods are the same as common functions. You can directly use classes and their member functions (do not forget to add header files.

--- Restore content end ---

Directory

  • General Creation Method
  • Export methods and call methods of common functions
  • Methods and call methods for exporting classes and their member functions

 

As we all know, we canClass and FunctionExport to form a. dll file for use by other programs. The following describes how to use it in the Qt environment.

To create a shared library, follow these steps:

In the next step, a class dialog box will appear. You don't need to worry about it. Just click Create and delete all the classes later.

After the creation, you will find a header file containing global, which defines _ declspec (dllexport), _ declspec (dllimport), and so on, you can also delete it (if you want to follow the following method, delete it ).

In the. pro file, add

CONFIG + = dll // even if your code is written as CONFIG + = staticlib, you need to change it.

Next, let's start creating a method!

 

Based on the dll export operation, there are two modes:

  • Export common methods(Static or dynamic call can be performed after export)

First, the header file content:

 

Header file: Add _ declspec (dllimport) in front of the method declaration. You can also use # define to define it. See the code: // to share a header file with future programs, when creating a dll, _ declspec (dllexport) is used. When using a dll, _ declspec (dllimport) is used. You can write down # define TESTDLLSHARED_EXPORT _ declspec (dllexport) separately) # ifdef shard # else # define TESTDLLSHARED_EXPORT _ declspec (dllimport) # endif // below I want to define four common functions: struct int test1 (); TESTDLLSHARED_EXPORT int test2 (void ); TESTDLLSHARED_EXPORT int test3 (int a); TESTDLLSHARED_EXPORT int test4 (int a, int B );

Then implement the methods test1, test2, test3, and test4 in the corresponding cpp source file (for simplicity, I only output one sentence ):

. Cpp source file: // remember to add the header file above // remember to add the iostream header file. Otherwise, cout cannot use int test1 () {std: cout <"test1" <std :: endl;} int test2 (void) {std: cout <"test2" <std: endl;} int test3 (int a) {std :: cout <"test3" <std: endl;} int test4 (int a, int B) {std: cout <"test4" <std :: endl ;}

Next, you can create it. After the creation is successful (the creation fails, check carefully, and the code has been verified), you will find a name consistent with the project name in your project output directory. dll file.

OK! Next we will use this dll:

There are two methods to call a dll: static call and dynamic call.

  • Static call

First, create a regular CIDR project, and copy the generated .dllto your project output directory (together with the .exe file );

Next, open the. pro file in the General C ++ project and add the detailed. dll file address. The format is as follows:

 

LIBS + = full name of the project output path \ dll file

For example, 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. You can change it when using it.

Next, we will create. copy the header file of the dll file to the current project path and add it to the project. Note: if you do not follow the # define condition definition method above, please rewrite _ declspec (dllimport ).

Next, include this header file in the. cpp file, and you can use the previous functions as much as you like, such as using test1 () directly, and so on.

  • Dynamic call

!!! Note: extern "C" or extern "C" is not used during creation. def file keeps the function name unchanged (it won't be used after many attempts. def file, will be welcome to leave a message), so the function name needs to be changed during dynamic calling! Because the compiler has changed the function name!

So, you will surely ask how we know what the function name in the dll has become? Don't worry, search for ". dll viewer ", which is everywhere. After downloading it, select the one we just downloaded. the dll file can be viewed. Below are the new names of our functions test1, test2, test3, and test4 (You may be different from mine ):

Please pay attention to the section in the red box (don't worry about anything else, there are other things in my file). These are the four functions in. dll file name, I guess a little bit, the following v represents the parameter is void type, the number of I represents the number of int type parameters, the first letter is not clear (note: the number of parameters is not correctly listed, but we only need the correct function name, without affecting ).

OK! This is easy! Steps:

Create a common C ++ project. The difference is that the "LIBS + = project output path \ dll file full name" statement is not added to the. pro file.

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

# Include <iostream> # include <windows. h> int main () {// first define the function pointer to receive the typedef int (CALLBACK * Fucv) () and typedef int (CALLBACK * Fuci) functions with different parameters) (int); typedef int (CALLBACK * Fucii) (int, int); // get. dll file handle. The header file is required for windows. HINSTANCE hdll = LoadLibrary (L "testDll. dll "); // L indicates a wide string. If L is not specified, an error occurs. For details, check the string. // be sure to use it here. function name in the dll file Fucv t1 = (Fucv) GetProcAddress (hdll, "_ Z5test1v"); Fucv t2 = (Fucv) GetProcAddress (hdll, "_ Z5test2v "); fuci t3 = (Fuci) GetProcAddress (hdll, "_ Z5test3i"); Fucii t4 = (Fucii) GetProcAddress (hdll, "_ Z5test4ii "); // now t1 executes the test1 function, and so on t1 (); t2 (); t3 (1); t4 (1, 2); FreeLibrary (hdll); return 0 ;}

The above is the method for creating. dll and using. dll in common functions. Does dynamic calling seem very troublesome? But it has many advantages (please check it yourself), and it is said that the function name here can be used. def file implementation does not change the name, saving a lot of trouble, but I tried a variety of methods, such as in. add in DEF_FILE in pro. def file is still not 55555. You can leave a message!

  • Export class and its member functions(It can be called statically after export. Currently, it is not dynamically called. In some books, classes do not support dynamic calling. on the Internet, it is said to write a method in the class to return class objects. However, I personally think this is wrong, because at this time, the function pointer defined by myself must be used to define the method of the returned class object, and it is impossible to succeed [verified]. Of course, there may be better methods. If you know this, please feel free to contact us)

For a class, add the defined _ declspec (dllexport) before the class name after the class; replace it with _ declspec (dllimport ), you can also refer to the # define condition definition method above. Example:

# Define export _ declspec (dllexport) # ifdef export # else # define HPTICKDLLSHARED_EXPORT _ declspec (dllimport) # endifclass export HpTickDll // I have defined the class HpTickDll {public: int Start (); // note that you do not need to add HPTICKDLLSHARED_EXPORT int GetTime (); private: LARGE_INTEGER li; LONGLONG start, end, freq; int useTime;} before the member function ;};

Static call methods are the same as common functions. You can directly use classes and their member functions (do not forget to add header files.

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.