Static Link Library for VC ++ dynamic link library (DLL) Programming
The explanation of the static link library is not the focus of this article, but before explaining the DLL, the example of a static link library can help us quickly build the concept of "library.
Figure 1 create a static Link Library
1. In VC ++ 6.0, create a new static library project named libTest (Click here to download the attachment of this project) and create a new lib. h and lib. cpp files: lib. h and lib. the source code of 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
# Include ".. \ lib. h"
# 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. In the code, # pragma comment (lib, ".. \ debug \ libTest. lib") means that the. obj file generated in this file should be connected with libTest. lib.
If you do not need to specify # pragma comment, you can directly set it in VC ++. 2. Select the tools, options, directories, library files menu, or options in sequence, and enter the path of the file to be imported. The red circle in Figure 2 shows the path of the libTest. lib file we added.
Figure 2 set the library file path in VC
The example of this static link library should at least let us understand what the library functions are and where they come from. We now have the following fuzzy understandings:
(1) The Library is not a monster. The programming of the library is not much different from the general programming, but the library cannot be executed separately;
(2) The Library provides some things that can be called by other programs. To call other programs, it must be indicated in some way that it is to be called.