Create a static library project named libtest in VC, and create two files, Lib. h and Lib. cpp.Source codeAs 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 sourceCodeAs follows:
# Include <stdio. h>
# 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 ));
}