The explanation of the static link library is not the focus of this article, but the example of a static link library can quickly help us to establish the concept of "library" before we specifically explain the DLL.
Figure 1 Creating a static link library
As shown in Figure 1, the new Static library project named Libtest in vc++6.0 (click here to download this project attachment) and create a new lib.h and lib.cpp two files, lib.h and Lib.cpp source code as follows:
extern "C" int Add (int x,int y); External functions declared as C compilation, connection mode
int add (int x,int y)
return x + y;
Compiling this project gives you a. lib file, which is a library of functions that provides the functionality of Add. Once the header file and the. lib file are submitted to the user, the user can use the Add function directly.
The C library functions in standard Turbo C2.0 (which we use for scanf, printf, memcpy, strcpy, etc.) come from this static library.
Let's take a look at how to use this library, a new Libcall project in the workspace where Libtest works. The Libcall project contains only one main.cpp file, which demonstrates the method of calling a static-link library with the following source code:
#include ". Lib.h "
#pragma comment (lib, "...). DebuglibTest.lib ")//Specifies to be connected with a static library
int main (int argc, char* argv)
printf ("2 + 3 =%d", add (2, 3));
Static link library call is so simple, maybe we use every day, but we do not understand the concept. In code #pragma comment (lib, "...). DebuglibTest.lib ") means that the. obj file generated by this file should be connected with libTest.lib.
If you do not #pragma comment specified, you can set up directly in VC + +, such as Figure 2, select Tools, Options, directories, library files menu or options, fill in the library file path. Figure 2 adds the red circle to the path of the LibTest.lib file that we added.
Figure 2 Setting the library file path in VC
The example of this static link library lets us understand at least what the library functions are and where they come from. We now have the following vague realizations:
(1) library is not a monster, the process of writing a library and writing a general program is not very different, but the library can not be implemented alone;
(2) The library provides something that can be invoked by another program, and other programs must call it in some way to indicate that it is to be invoked.