Create your own DLL and Lib files

Source: Internet
Author: User

Third-party libraries are often used. For example, glut, FreeType, SDL, and cegui and OSG.

Although these libraries are open-source, they are often used and are not modified.

Three things are involved: the. H. Lib. dll file.

There are three main modes:

. H +. Lib

. H +. Lib +. dll

. Dll

 

The second is the most common, with fewer of the other two

 

1. Implement a DLL and Lib by yourself:

Http://msdn.microsoft.com/en-us/library/ms235636 (vs.80). aspx

 

Note that my compiler is vs2005.

 

1. Create a Win32 console application, enter the project name, click OK, and click Next. Select the application type: DLL additional option: Empty Project. Click OK

2. Add the header file mathfuncsdll. H to the empty project. The content is as follows:

// Mathfuncsdll. h </P> <p> namespace mathfuncs <br/>{< br/> class mymathfuncs <br/>{< br/> public: <br/> // returns a + B <br/> static _ declspec (dllexport) Double add (double A, double B ); </P> <p> // returns a-B <br/> static _ declspec (dllexport) Double subtract (double A, double B ); </P> <p> // returns a * B <br/> static _ declspec (dllexport) Double multiply (double A, double B ); </P> <p> // returns a/B <br/> // throws dividebyzeroexception if B is 0 <br/> static _ declspec (dllexport) double divide (double A, double B); <br/>}; <br/>}

3. Add the mathfuncsdll. cpp file as follows:

// Mathfuncsdll. CPP <br/> // compile with:/ehs/ LD </P> <p> # include "mathfuncsdll. H "</P> <p> # include <stdexcept> </P> <p> using namespace STD; </P> <p> namespace mathfuncs <br/> {<br/> double mymathfuncs: add (double A, double B) <br/>{< br/> return a + B; <br/>}</P> <p> double mymathfuncs: Subtract (double A, double B) <br/>{< br/> return a-B; <br/>}</P> <p> double mymathfuncs: multiply (double A, double B) <br/> {<Br/> return a * B; <br/>}</P> <p> double mymathfuncs: Divide (double A, double B) <br/>{< br/> If (B = 0) <br/>{< br/> throw new invalid_argument ("B cannot be zero! "); <Br/>}</P> <p> return a/B; <br/>}< br/>}

4. Set project properties:

In Solution Explorer on the left, right-click the project name. In the displayed dialog box, click "Left". Set Properties:/General. "configuration type on the right": select Dynamic library (. dll)

Click Generate:/to generate mathfuncdll.

 

Now you can go to the debug folder and check the mathfuncsdll. dll mathfuncsdll. Lib files that we are most concerned about. Note that the mathfuncsdll. Lib file is only 3 kb in size.

 

2. Use DLL and Lib files

Create an empty Win32 console application: usemathdll project name

Add the CPP file myexecrefsdll. cpp:

# Include <iostream> </P> <p> # include "mathfuncsdll. H "</P> <p> # pragma comment (Lib," mathfuncsdll. lib ") // import the Lib file </P> <p> using namespace STD; </P> <p> int main () <br/>{< br/> double A = 7.4; <br/> int B = 99; </P> <p> cout <"A + B =" <br/> mathfuncs: mymathfuncs: add (a, B) <Endl; <br/> cout <"a-B =" <br/> mathfuncs: mymathfuncs: Subtract (a, B) <Endl; <br/> cout <"a * B =" <br/> mathfuncs: mymathfuncs: multiply (a, B) <Endl; <br/> cout <"A/B =" <br/> mathfuncs: mymathfuncs: Divide (a, B) <Endl; </P> <p> system ("pause"); <br/> return 0; <br/>}

Then, put the mathfuncsdll. lib and mathfuncsdll. H files in the same directory as myexecrefsdll. cpp, that is, the current directory. Set mathfuncsdll. dll can be put under system32. Of course, the simplest way is to put it in the debug or release folder of usemathdll. The later generated EXE can find the desired DLL file in the current directory.

Now you can click "run:

The result is as follows:

A + B = 106.4 <br/> A-B =-91.6 <br/> a * B = 732.6 <br/> A/B = 0.0747475 <br/> press any key to continue...

 

3. directly use the. h and Lib files

Open the mathfuncsdll project and set the project properties according to entry 1. However, select the static library (. Lib) for the final configuration type)

Then generate mathfuncsdll and go to debug to check whether the mathfuncsdll. Lib file is changed to 29 KB.

Then replace mathfuncsdll. Lib with the original mathfuncsdll. IB file under the usemathdll project directory, and delete mathfuncsdll. dll under debug, so that usemathdll can still run normally.

OK, that is all.

Hope will be helpful for you

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.