Detailed description of generation and use of. dll and. lib files in c ++

Source: Internet
Author: User

Detailed description of generation and use of. dll and. lib files in c ++

--------------------------------------------------------------------------------

Two types of libraries:

• Contains information about the DLL file where the function is located and the Function Location (entry) in the file. The code is provided by the DLL loaded in the process space during runtime, known as the dynamic link library.
• Contains the Function Code itself. during compilation, the code is directly added to the program, called the static link library of the static link library.
There are two Connection Methods:

• The Dynamic Link uses the dynamic link library to permit the execution of the dig (.dll file or the .exe file) only contains the information required to locate the executable code of the DLL function at runtime.
• Static links use static link libraries. The linker obtains all referenced functions from the static Link Library LIB and puts the libraries and codes together in the executable files.

--------------------------------------------------------------------------------

Differences between the two files

--------------------------------------------------------------------------------

Note the following two files when using lib:

• The. h header file contains the class or symbol prototype or data structure described in lib. When an application calls lib, the file must be included in the application's source file.
•. LIB file.

When using dll, pay attention to three files:

• The. h header file, which contains a. h file indicating the output class or symbolic prototype or data structure in the dll. When an application calls a dll, the file must be included in the application's source file.
•. The LIB file is generated after the dll is compiled and linked successfully. It is used to introduce the file to the application when other applications call the dll, otherwise, an error occurs. (If you do not want to use the lib file or do not have the lib file, you can use the WIN32 API functions LoadLibrary and GetProcAddress to load the file ).
• The dll file is a real executable file. Only the. exe file and. dll file are available when the application program behind the successful development is completed, and the. lib file and. h header file are not required.

--------------------------------------------------------------------------------

Generation and usage

--------------------------------------------------------------------------------

Operating System: WIN7

Software Development: VS2010

--------------------------------------------------------------------------------

1. Generate the lib File

First, create a console Project (New> Project> console Program) and add. cpp and add. H files.

Copy codeThe Code is as follows: // sub. h
# Ifndef _ SUB_H
# Define _ SUB_H
Void sub (int a, int B );
# Endif

// Sub. cpp
# Include "sub. h"
# Include <iostream>

Void sub (int a, int B)
{
Std: cout <(a-B) <std: endl;
}

Because there is no main () function in the project, a direct compilation error occurs. In this case, right-click the project, select project properties, and select static Link Library.

Press F7 and build solution to generate the lib file. Only the. lib file is generated in Debug.

2. Generate the dll file

The process of generating the dll file is the same as the above process. You only need to select Dynamic Library (. dll. A. lib and. dll files are generated in Debug.

--------------------------------------------------------------------------------

3. Use of the two files

When using the static link library, you only need to add the. h and. lib files to the project folder. The dynamic link library should add the. h,. lib and. dll files to the project.

Copy codeThe Code is as follows: # include <iostream>
# Include "sub. h" // header file of the Linked Library
Using namespaces std;

# Pragma comment (lib, "sub. lib") // Add the Link Library

Int main ()
{
Sub (5, 4 );
Return 0;
}

4. Use the. dll file only

When there are no. h and. lib files, you need to load the function pointer and WIN32 API functions LoadLibrary and GetProcAddress. You only need the. dll file (put the. dll file in the project directory ).

Copy codeThe Code is as follows: # include <iostream>
# Include <windows. h> // use functions and some special Variables
Typedef void (* DLLFunc) (int, int); // determines the form parameter for calling the Function
Int main ()
{
DLLFunc dllFunc;
HINSTANCE hInstLibrary = LoadLibrary ("sub. dll"); // load. dll

If (hInstLibrary = NULL)
{
FreeLibrary (hInstLibrary );
}
DllFunc = (DLLFunc) GetProcAddress (hInstLibrary, "sub"); // The second parameter is the name of the function to be called.
If (dllFunc = NULL)
{
FreeLibrary (hInstLibrary );
}
DllFunc (5, 4 );
FreeLibrary (hInstLibrary );
Return (1 );
}

I have been engaged in API recently. I have used curl, tinyxml, and jsoncpp open-source libraries. It is very convenient to use in linux. I have encountered some problems in compiling on windows, learn from the new organization ~~

Related Article

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.