Stepped pit Documentary (1): the development of a small section of the algorithm in Qt, how to create a dynamic link library DLL?

The landlord has been developed under a Qt GUI software environment, we all know, is to write C ++ code in Qt Creator (Qt official IDE). These days involves a background data processing algorithm, we need to make it into a dynamic link library DLL available to others to use. In fact, this is a so easy thing, but the first time, stepped on a few pit, this process is recorded.


First enumerate and selection methods, there are so few substantially:

  1. Dependent module in Qt Creator QtCore, personal understanding is that a lot of container in native C ++ have been re-packaged in Qt, it is necessary to rely on this module.
  2. Not dependent on any module in Qt Creator, for the production of pure C ++ code DLL libraries.
  3. Making DLL in VS.

Factors to consider when selecting methods:

  1. Since it is for other people to use the library as a third party, of course, universal, and therefore do not want to rely on what feels natural in any Qt. So to make DLL native C ++ code.
  2. Considered for production development and DLL library C ++ code in VS, is clearly a more mainstream way to Current. Thus the choice of three methods.

Remarks:

  1. All these three methods can be found online graphic description, only records I stepped pit yourself here.
  2. In fact, whether or not dependent on QtCore such modules, and ultimately generate a DLL library certainly are binary executable code. But developers to write code that is different from the middle of the compilation phase certainly have helped us do a deal to ensure that the binary code is OK.

After determining the good direction, then began to do it ~


I use the DLL library process is divided into three phases:

  1. First you load the application to third-party libraries, library load
  2. Under the premise to load the library a success, further to resolve the function symbol library, it is to find the function interface is generally a function pointer
  3. After finding the address of the function, you can call and execute a function, which entered the body of the function

Landlord in two stages 2 and 3 are stepped pit.

In fact, in the beginning I was in first try Qt Creator made a DLL, and then calls itself to do this DLL in Qt Creator. To the dynamic linker run-time library, the result library load successfully, but failed to resolve the function. I do not have specific reasons to the investigation, suspect that it was inside the DLL function name has changed, tried to use some tools to view specific function inside the DLL, they did not practicable, give up.


Then turned to the next VS. At this time produced the DLL has been able to resolve the function was successful, but the program is running Shique collapsed, suggesting that I segment error has occurred.


So I start debugging the code in VS. PS: and this was the biggest stepped pit.


The same piece of code, interpreted in different IDE compiler really different, to check the syntax is different.


After debugging, VS under their own test cases have been run through.


But my application or call the DLL mistakes. At this very puzzled, but quickly thought of in my past experience, the mistakes tend to be something to do with the object space, so I thought of a hidden error.


My DLL libraries, because C ++ code, that several data processing functions are encapsulated into a class member functions, while the application is running to bypass the class object directly call a member function, which is obviously wrong because the member function must be called by the instance of the class. Think about the space object's class does not exist, how can it call its member function, which is the cause of the error segment.


So this can be said that their existence in the design of the library issues.


Finally removed the class, directly exposed to the function interface, the problem is solved. PS: this is a bit of style C, ha ha ~

Published 34 original articles · won praise 22 · views 7142

Guess you like

Origin blog.csdn.net/lizun7852/article/details/103937800