How to use Lib Library DLL library and its relationship

Source: Internet
Author: User

First, Lib Library

There are two types of Lib libraries: static lib, or the most common Lib library, which adds code directly to the program at compile time. In static Lib, a Lib file is actually a collection of any obj files, and the obj file is generated by the CPP file compilation.

The other Lib contains the DLL file where the function resides and the information (ingress) of the function location in the file, which is provided by the DLL that is loaded in the process space at run time. That is, usually write DLL when the Lib, where Lib is only a DLL, is the DLL exported function list file only.

Common denominator: Both are binary files, are called at link time, using the static Lib Exe can run directly, using the other Lib exe needs the corresponding DLL to run.

Example of creating a static Lib library:

1.vs2005 (contains) the above version

2. Establish Win32 Console Project

3. Next select the Static library

4. Complete

Code:

1 //Mylib.h2 #ifndef Mylib_h_h3 #defineMylib_h_h4#include"stdafx.h"5 6 voiddisplay ();7 voiddisplay1 ();8 voiddisplay2 ();9 Ten #defineA (x) (x) * (x) One  A #endif

  

1 //Mylib.cpp2#include"stdafx.h"3#include"Mylib.h"4 5 voiddisplay ()6 {7cout<<"This is display"<<Endl;8 }9 Ten voiddisplay1 () One { Acout<<"This is display1"<<Endl; - } -  the voiddisplay2 () - { -cout<<"This is display2"<<Endl; -}

After compiling, generate a mylib.lib library file that references the library file as follows:

Right-click the project--Properties (ALT+F7)-->c/c++--> attach the Include directory---> include the header file that you want.

Right-click Project--Properties (ALT+F7)--linker--General---> Additional directories---> contains the directory where Lib library resides

Right-click the project--Properties (ALT+F7)--Input---> Additional dependencies--->lib library name

So you can refer to the function in the Lib.

  

1#include"stdafx.h"2#include"Mylib.h"3 4 int_tmain (intARGC, _tchar*argv[])5 {6 display ();7 display1 ();8 display2 ();9Cout<<a (6-2) <<Endl;Ten GetChar (); One     return 0; A}

Of course, you can also use the #pragma comment (lib, "LibPath") method to invoke the Lib file, and you must include the corresponding header file.

Another way to use LIB libraries: using static libraries Consistent, the only difference is that the runtime to add the corresponding DLL.

DLL usage:

DLL actually and EXE is almost exactly the same, the only difference is EXE's entry function WinMain function (the console program is the main function), and the DLL is the DllMain function, the others are exactly the same. So someone also jokingly DLL is unable to run its own exe. DLL creation is also relatively simple the only trouble is to define the export function interface.

DLL creation process:

To set up the Win32 console project, in the application setup steps, select "Dynamic Library" to complete. (this is only slightly different in the way of the simplest DLL,WIN32 application)

There are two ways to define an exported function interface:

1. Using the __DECLSPCE macro

  

1 //dllmain.cpp: Defines the entry point for the DLL application. 2#include"stdafx.h"3 4 BOOL apientry DllMain (hmodule hmodule,5 DWORD Ul_reason_for_call,6 lpvoid lpreserved7                      )8 {9     Switch(Ul_reason_for_call)Ten     { One      CaseDll_process_attach: A      CaseDll_thread_attach: -      CaseDll_thread_detach: -      CaseDll_process_detach: the          Break; -     } -     returnTRUE; -}
View Code

1 //Mydll1.h2 3 #ifndef Mydll_h_h4 #defineMydll_h_h5 6 extern "C"__declspec (dllexport)voidDisplayvoid);7 extern "C"__declspec (dllexport)voidDisplay1 (void);8                                  voidDisplay2 (void);9 Ten #endif
View Code

1 //Mydll1.cpp2#include"stdafx.h"3#include"Mydll1.h"4 5 voidDisplayvoid)6 {7cout<<"Call display ()"<<Endl;8 }9 Ten voidDisplay1 (void) One { Acout<<"Call display1 ()"<<Endl; - } -  the voidDisplay2 (void) - { -cout<<"Call display2 ()"<<Endl; -}
View Code

Mydll1.h in the header file:

  

1 extern " C " void display (void);

The extern "C" here means that we are going to compile the function in C language, preventing the compiler from compiling a function name error in C + +, because there is no overload in C and the overload is allowed in C + +, so the function compiled in C + + will appear in the form of [email protected] ; Having the compiler compile in C can guarantee that C can invoke the C + + dynamic-link library. The function that __declspec (dllexport) represents is the exported function interface of the DLL. Interfaces that are not exported are not available and are different from static Lib libraries, and all functions, macro definitions, and so on in the static Lib library can be used.

2. Using the Def file, the file functions like __declspec (dllexport).

Add a def file under the resource file:

  

Libraryexports    Display    display1

DLL is called:

1.dll Display Invocation: If we have to know the function name, return value, parameter list. The corresponding header file is not needed at this time, and the Lib file is not required. You just need the corresponding DLL.

  

1 //test_my_dll.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"windows.h"6 7 8 int_tmain (intARGC, _tchar*argv[])9 {TenHINSTANCE hinstance = LoadLibrary ("Mydll1.dll"); Onetypedefvoid(*_print) (void); A _print Printfun; -     if(HInstance! =NULL) -     { thePrintfun = (_print) GetProcAddress (HINSTANCE,"Display1"); -     } - Printfun (); - FreeLibrary (hinstance); +System"Pause"); -     return 0; +}

In this case: hinstance hinstance = LoadLibrary ("Mydll1.dll"), the "2 IntelliSense:" Const char * "Type of real participation" LPCWSTR "type of parameter incompatibility" issue. WORKAROUND: Engineering---> Properties---> Normal-to-character Set---> Use multibyte character sets.

implicit invocation of 2.dll

This is the corresponding Lib library comes in handy, and its usage is consistent with the static library usage.

Summary: Show call and implicit call these two methods are different, show the advantage of the invocation is that as long as the interface parameter list does not change, modify the function implementation of the details also do not matter, the calling EXE program does not need to recompile just replace the new version of the DLL. It is easy to use in large projects, but the call process is relatively complex and requires a series of Windows functions, function pointers, and so on. It is easy to understand the implicit invocation and the static library usage, and the disadvantage is that the corresponding EXE needs to be recompiled whenever any content is modified.

Resources:

Http://www.cppfans.org/1394.html

http://blog.csdn.net/qq419036154/article/details/6438539

http://blog.csdn.net/tianhen791/article/details/7209740

How to use Lib Library DLL library and its relationship

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.