A detailed explanation of the generation and use of the. dll and. lib files in C + + _c language

Source: Internet
Author: User
Tags win32

A detailed explanation of the generation and use of the. dll and. lib files in C + +

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

Two types of libraries:

• Contains information about the DLL file where the function is located and the location of the function in the file (the entry), which is provided by the DLL loaded in the process space by the runtime, known as the dynamic link library.
• Include the function code itself, adding code directly to the program at compile time, called the Static link library.
There are two ways of linking:

• Dynamic linking uses dynamic link libraries, allowing executable modules (. dll files or. exe files) to contain only the information needed to locate the executable code for the DLL function at run time.
• Static link using a static link library, the linker obtains all referenced functions from the static link library lib and places the library with the code in the executable file.

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

The difference between two kinds of files

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

Use Lib to note two files:

A .h header file that contains a class or symbol prototype or data structure that describes the output in Lib. When the application calls Lib, the file needs to be included in the application's source file.
•. LIB file.

You need to note three files using DLLs:

.h header file that contains the. h file of the class or symbol prototype or data structure that describes the output in the DLL. When an application invokes a DLL, the file needs to be included in the application's source file.
•. LIB file, is the DLL in the compilation, link after the successful production of the file, the role is when other applications call the DLL, you need to introduce the file into the application, or create an error (if you do not want to use Lib file or no Lib file, you can use the WIN32 API function LoadLibrary, GetProcAddress load).
DLL files, real executables, when a successful application is published, only the. exe file and the. dll file are required, and No. lib file and. h header files are required.

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

How to build and use

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

Operating system: WIN7

Development software: VS2010

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

1. Generate Lib File

First, we build a console project (new-> Project-> console program), add Add.cpp and add.h files.

Copy Code code 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. At this point, to right click on the project, and select Engineering Properties, the following figure, select the static link library can be.

Then press F7,build solution to produce lib file. Only. lib files are generated in Debug.

2. Generate DLL file

The process of building a DLL file is the same as the previous procedure, except that the dynamic Library (. dll) is selected. One of the. lib and. dll files is generated in Debug.

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

3. Use of two kinds of documents

When used, the static link library simply adds the. h and. lib files to the engineering folder. Dynamic link Libraries Add. h,. lib, and. dll files to the project.

Copy Code code as follows:

#include <iostream>
#include the header file for the Sub.h//link library
Using namespaces std;

#pragma comment (lib, "Sub.lib")//Join link library

int main ()
{
Sub (5,4);
return 0;
}


4. How to use only. dll files

When there are no. h and. lib files, function pointers and WIN32 API functions are required LoadLibrary, GetProcAddress mounts, and only. dll files are required (the. dll file into the engineering directory).

Copy Code code as follows:

#include <iostream>
#include <windows.h>//Use functions and certain special variables
typedef void (*DLLFUNC) (int,int); To determine the parameters of the calling 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 invoke
if (DllFunc = NULL)
{
FreeLibrary (hinstlibrary);
}
DllFunc (5,4);
FreeLibrary (hinstlibrary);
return (1);
}


Recently has been engaged in the API this thing, with Curl, TinyXML and jsoncpp several open source libraries, in Linux is very convenient to use, under Windows or their own compiler encountered some problems, the new finishing study, spare ~ ~

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.