[Post] differences between static library (LIB) and dynamic library (DLL)

Source: Internet
Author: User

Static Connection Library is to directly link the function code used in the (LIB) file to the target program. Other library files are no longer needed when the program is running; dynamic Link is to link the file module (DLL) where the called function is located and the location of the called function in the file to the target program. When the program is running, find the corresponding function code from the DLL, therefore, support for the corresponding DLL files is required.

Static and dynamic libraries share code, all commands in lib are directly included in the final generated EXE file. However, if a DLL is used, the DLL does not need to be included in the final EXE file. During execution of the EXE file, the DLL file can be dynamically referenced and detached. Another difference between a static link library and a dynamic link library is that the static Link Library cannot contain any other dynamic or static Link Library, other dynamic or static link libraries can be included in the dynamic link library.

"Each lib file is defined by several functions (assuming only functions"
There are two types of LIB libraries: one is to include information about the DLL file where the function is located and the location of the function in the file, known as export and warehouse; the other is to include the Function Code itself, generally the existing DLL, the former database is used; the former TC/BC in DOS is the latter one. The header file (. h) contains the function prototype declaration ).

"After the # include header file containing these function declarations, our application can use the functions in the Lib file"

You also need to specify the compiler to link the corresponding library file. In the IDE environment, generally all the library files used are specified at a time, and the compiler looks for the libraries required by each module. In the command line compiling environment, the library called by each module needs to be specified.

"What is the difference between the file that directly gives the function definition, such as The. cpp file, and the header file? What is the use of the static link library"
The CPP file is the source code, and the library file is the compiled Binary Code. For example, you can call the Windows API but cannot see the same source code.

"What I still don't understand is that as long as the Lib file in the static link library is used, the content of the entire lib file is put into the EXE file, is it compiled or linked?"
Link lib to the target code.

Static link library (LIB)
In VC ++ 6.0, a new static library project named libtest is created,

Create the Lib. h and Lib. cpp files. The source code of Lib. h and Lib. cpp is as follows:

// File: Lib. h
# Ifndef lib_h
# Define lib_h
Extern "C" int add (int x, int y); // declared as an external function in C compilation and Connection Mode
# Endif

// File: Lib. cpp
# Include "Lib. H"
Int add (int x, int y)
{
Return X + Y;
}

Compile the project to obtain a. Lib file, which is a function library and provides the Add function. After the header file and the. Lib file are submitted to the user, the user can directly use the Add function.

The C library functions (scanf, printf, memcpy, strcpy) in the standard Turbo c2.0 come from this static library.

Next let's take a look at how to use this library and create a new libcall project in the workspace where the libtest project is located. The libcall project contains only one main. cpp file, which demonstrates the call method of the static Link Library. Its source code is as follows:

# Include <stdio. h>
# Include ".. \ Lib. H" // cannot be lost
# Pragma comment (Lib, ".. \ debug \ libtest. lib") // specify the link to the static library
Int main (INT argc, char * argv [])
{
Printf ("2 + 3 = % d", add (2, 3 ));
}

The call to the static Link Library is so simple that we may be using it every day, but we do not understand this concept. # Pragma comment (Lib, ".. \ debug \ libtest. lib") in the Code indicates that the. OBJ file generated in this file should be connected with libtest. Lib.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/okaimee/archive/2010/03/18/5392684.aspx

 

 

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.