Ways to encapsulate classes into dll/lib

Source: Internet
Author: User

If your work has long been associated with a field, such as a long-term development of direct volume rendering (DVR), you may often use your own transfer function class, If you add the. h and. cpp files of the transfer function class to each project, you'll be in trouble, in fact, we can use your own class as you would with the OpenGL library, by encapsulating your written class into a DLL, as follows:

The first step: Making DLLs
Using VC6 to create a new project, select the Win32 Dynamic-link Library (Empty project), and then add the header file and CPP file. Suppose you want to encapsulate the class name into Transferfunction, add header Files TransferFunction.h and TransferFunction.cpp to the project. and modify the TransferFunction.h into:
Class __declspec (dllexport) transferfunction
{
...
}
This indicates which class is to be exported from the DLL later. This will result in the TransferFunction.dll and TransferFunction.lib two files being compiled.

Step Two: How to use this DLL
When a DLL has been built, there are two ways to invoke classes and member functions in the DLL in other programs:

method One:
1 copy TransferFunction.dll and TransferFunction.lib to the execution path of the calling program, and note that it is not under the debug path.
2) Add TransferFunction.lib to Project->setting->link (or #pragma comment (lib, "TransferFunction.lib")
3) Convert the __declspec (dllexport) in TransferFunction.h to __declspec (dllimport)
It is then copied to the execution path of the calling program.

4 Finally, the Transferfunction class and its member functions can be used in the main program by including TransferFunction.h.

method Two (recommended):

In method one, every time you build a project you need to put transferfunction.dll,transferfunction.lib, TransferFunction.h three copies of files to the project, in fact, this only plays the DLL to the class code protection function, and not much reduce the programmer's workload, the following method can reduce the workload of programmers, the specific steps:

1: Throw the TransferFunction.dll under the Windows/system32

2: Will. Transferfunctionlib threw it into the VC's lib directory.

3: Throw the TransferFunction.h in the method one into the VC's include directory

4: The last step of the same method one.

You can then use a reusable class of your own encapsulation like the glut, Glui, and other libraries in OpenGL.

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.