How to generate and Load Static and Dynamic Databases

Source: Internet
Author: User

How to generate and Load Static and Dynamic Databases
When we used to write C language code, we basically had to include the corresponding header file to use some functions. For example, when we used the printf function, stdio was the most commonly used one. h header file, stdio. h has our printf Declaration, and its real implementation is in our underlying library.
First, let's take a look at what is a database?
Library: A function library is a set of functions and binary data code that implement a certain type of functions.
Note: although the library is a set of binary code of the function, it cannot be executed independently and can be loaded into the memory for execution in conjunction with other programs.
Windows static library: xxx. lib
Dynamic library: xxx. dll
Linux static library: xxx.
Dynamic library: xxx. so

The static library and dynamic library mentioned here refer to different link times. First, let's review the compilation process. Let's compile a common program into an executable program: preprocessing, compilation, assembly, and link.

In fact, the first three steps are the basic C language code that needs to be run. The xxx. o file is generated and eventually developed in three directions => executable files, static libraries, or dynamic libraries.

I. Static Library
Static libraries are called static libraries because the code of our function library is loaded into executable files at the compilation stage. Because all data in the entire function library is integrated into the target code, its advantages and disadvantages are obvious.
Advantage: The compiled execution program does not require the support of external function libraries.
Disadvantage: The generated file is relatively large. If the code of the function library changes, it needs to be re-compiled.
Features: the code of the static library has been loaded into the executable program during compilation, so the generated executable program is large.

The following describes how to use VS2008 to create and call a static Library:
1. Create a project -- select a Win32 project and enter the project name.

Select static library in application settings

In this way, the project is created.

2. Add two files, one for lib. cpp and the other for the header file lib. h. The function we compile is placed in lib. cpp, and its prototype is placed in lib. h. The content of these two files is:

 
 
  1. //lib.cpp
  2. #include "stdafx.h"
  3. #include "lib.h"

  4. int add(int x,int y)
  5. {
  6. return x + y;
  7. }
 
 
  1. //lib.h
  2. #ifndef LIB_H
  3. #define LIB_H

  4. extern "C" int add(int x,int y);

  5. #endif
Note: Some users may. the expression of extern "C" in h "has doubts. In fact, it is a compilation Convention. extern is a keyword attribute, which represents the scope (visibility) attribute of variables, functions, and other types, this keyword tells the compiler that the declared functions and variables can be used in this module or file and other modules or files. "C" represents the compiler Connection Specification. Therefore, it can be understood as the Compilation instruction of mixed programming in C ++/C.
After compilation, you can view the compiled results in the Release or Debug folder Based on the Compilation configuration type.


3. the following describes how to use this function to create a console application and. h and the generated "win32_Lib.lib" are copied to the source folder of the new project, and then the test application source program is written as follows:
 
 
  1. // Test_Lib.cpp: defines the entry point of the console application.
  2. # Include
  3. # Include "lib. h" // function prototype Declaration

  4. # Pragma comment (lib, "win32_Lib.lib"); // import the static library

  5. Int main ()
  6. {
  7. Printf ("2 + 3 = % d", add (2, 3 ));
  8. Getchar ();
  9. Return 0;
  10. }
Note: Some people may not have seen the "# pragma comment" command, so they do not know what it means. In fact, the "# pragma" command is the most complex of all preprocessing commands, it is used to set the status of the compiler or to instruct the compiler to complete some specific actions. The original sound is: # pragma comment (comment-type, "commentstring"). comment-type is a predefined identifier. The annotation type should be compiler, exestr, lib, one of linker; commentstring is a string that provides additional information for comment-type.
# Pragma comment (lib, "win32_Lib.lib"); indicates link to the win32_Lib.lib library. It works the same as writing win32_Lib.lib to the Project settings. However, when using your code, you do not need to set the project settings.

Ii. Dynamic library
As the name implies, you will know by name that our dynamic library must be dynamically linked. Yes, our dynamic function library is not compiled into the target code during compilation. Instead, it is used to link the functions used in our function library at runtime. Therefore, the executable files generated by the dynamic function library are relatively small. Because the function library is not integrated into your program, but dynamically applied for and called when the program is running, the corresponding library and library path must be provided in the running environment of the program.
Features: the code of the dynamic library is loaded into the memory only when the executable program runs. During the compilation process, only simple references are provided. Therefore, the executable code is small in size.

The following describes how to use VS2008 to create and call a dynamic library:
1. Create a project -- select a Win32 project and enter the project name win32_Dll.
Select static library in application settings

In this way, the project is created.

2. Add two files: win32_Dll.cpp and win32_Dll.h. The function we wrote is placed in win32_Dll.cpp, and its prototype is placed in win32_Dll.h. The content of these two files is:
 
 
  1. // Win32_Dll.cpp: defines the export function of the DLL application.
  2. # Include "stdafx. h"
  3. # Include "win32_Dll.h"

  4. # DefineEXPORTING_DLL

  5. Int add (int x, int y)
  6. {
  7. Return x + y;
  8. }
 
 
  1. //win32_Dll.h
  2. #ifndef WIN32_DLL_H
  3. #define WIN32_DLL_H

  4. #ifdef EXPORTING_DLL
  5. #define API_TYPE__declspec(dllexport)
  6. #else
  7. #defineAPI_TYPE__declspec(dllimport)
  8. #endif

  9. extern "C" int API_TYPE add(int x,int y);
  10. #endif
Note: Some people may not have seen "_ declspec (dllexport)" and "_ declspec (dllimport)", so they do not know what it means. Actually, one is a provider, one is the user, and the interface between the two is the header file. The header file declares the method. In the provider, the method should be declared as "_ declspec (dllexport)". In the user, the method should be declared as "_ declspec (dllimport )". The two use the same header file as an interface and use Conditional compilation: Define a variable and set different values for the provider and user.

After compilation, you can view the compiled results in the Release or Debug folder Based on the Compilation configuration type.


3. the following describes how to use this function to create a console application and copy the generated "win32_Dll.dll" and "win32_Dll.lib" to the source folder of the new project, then write the test application source code as follows:
  
  
  1. # Include
  2. # Include

  3. Typedef int (* lpAddFun) (int, int); // function pointer

  4. Int main ()
  5. {
  6. HINSTANCEhDll;
  7. LpAddFun addFun;
  8. HDll = LoadLibrary ("win32_Dll.dll"); // load the dynamic library
  9. If (hDll)
  10. {
  11. AddFun = (lpAddFun) GetProcAddress (hDll, "add ");
  12. If (addFun)
  13. {
  14. Printf ("2 + 3 = % d \ n", addFun (2, 3 ));
  15. }
  16. }
  17. Getchar ();
  18. FreeLibrary (hDll); // uninstall the dynamic library
  19. Return 0;
  20. }
Note: Some of you may have never used function pointers, so it is hard to understand the code segment. For more information, see my previous blog: http://blog.chinaunix.net/uid-31439230-id-5763256.html;
The LoadLibray () function loads the specified dynamic link library and maps it to the address space used by the current process. Once loaded, you can access the resources saved in the library;
GetProcAddress () Han Shan Temple retrieves the function address of the output library in the specified dynamic link library (DLL). If the function is called successfully, the return value is the address of the output function in DLL. If the function fails, the return value is NULL.



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.