Before explaining the dynamic link library DLL, let's first explain the static Link Library. The example of a static link library can help us quickly build the concept of "library.
Example of a static Link Library
First, we use VC ++ to create a static Link Library, use VC ++ to create a Win32 static library project, and create two files libtest. h and libtest. cpp. The Code is as follows:
// File libtest. h
# Ifndef lib_h # define lib_h extern "C" int add (int x, int y); // declare as an external function of C compilation and connection mode # endif
// File libtest. cpp
#include "LibTest.h" int add(int x,int y) { return x + y; }
Compile the project directly and obtain a. Lib file under the DEBUG directory under the project directory. This file is a function library. How can we use it?
------------------------------------------------------------------------ Gorgeous split line --------------------------------------------------------------------------
Next, we will create a simple Win32 console application project test, which has only one test. cpp file. The Code is as follows:
// File test. cpp
# Include <stdio. h> # include ".. \ libtest. H "# pragma comment (Lib ,".. \ libtest \ debug \ libtest. lib ") // specify to connect int main (INT argc, char * argv []) {printf (" 2 + 3 = % d ", add (2, 3 ));
return 0; }
Note that # pragma comment (Lib ,".. \ debug \ libtest. lib ") indicates that this file is generated. the OBJ file and the libtest file generated just now. link lib together.
If you do not need to specify the # pragma comment, you can directly set it in VC ++, select Tools> Options> directories> library files menu or options, and enter the path of the file to be imported.
Run the program. The result is as follows: