Creation and use of Windows libraries: static libraries + dynamic libraries
First, the creation and use of static library 1.Static Library Creation
(1) first create the project test , the test code is as follows:
1) test.h
2) Test.cpp
#include "Test.h" #include <stdio.h> void Test_print () { printf ("Test_print in static lib."); } |
3) Right-click the project Test: Properties--> Configuration Properties--> General--> Configuration type: Select a static library Lib
4) Compile the project(build) and then generate test.libunder Debug in the solution .
2.using a static library= Libfile+ header File
1) Build a new project Uselib
2) Click on the resource file--> Add Existing Item--> join the library you just created test.lib
3) Right-click Properties-->vc++ Directory--> include directory: add test.h in the directory
The project code is as follows:
1) main.cpp
#include "Test.h" #include <stdio.h> int Main () { Test_print (); GetChar (); } |
Debug to see that the call was successful.
Second, the creation and use of dynamic Library 1. Static loading of dynamic libraries
1) Create a project static_load
Static_dll.h
//Export class class __declspec ( dllexport ) static_test { public : void print (); }; //export function __declspec (dllexport ) void static_function (); //Export object extern __declspec ( Span style= "Color:rgb (0,0,255)" >dllexport ) static_test static_objtest; |
Static_dll.cpp
#include "Static_dll.h" #include <stdio.h> void static_test::p rint () { printf ("static_test::p rint () in static load dll.\n"); } void static_function () { printf ("static_function in static load dll.\n"); } Static_test static_objtest; |
__declspec (dllexport) represents an export item as a dynamic library, otherwise a variable or function or class is not found. Configuration type selection: Dynamic library (DLL).
2) static load mode using dynamic Library
First create a dll.h
//Guide into class class __declspec (dllimport) static_test { Public: void print (); }; //Guide into function __declspec (dllimport) void static_function (); //Guide into Object extern __declspec (dllimport) static_test static_objtest; |
__declspec (dllimport) indicates that the item is an external import item imported from a dynamic library.
Create a test engineering uselib
Main.cpp
#include " dll.h " #include <STDIO.H> int main () { static_test st; st.print (); static_function (); static_objtest.print (); getchar (); } |
Both static_load.lib and static_load.dll need to be placed in the same directory , and the . lib The file is imported into the resource file.
2 Dynamic load Dynamic Library
1. Establish engineering Dynamic_load for the difference treatment here
Dynamic_dll.h
Export class class __declspec (dllexport) dynamic_test { Public: void print (); }; Export function __declspec (dllexport) void dynamic_function (); Exporting objects extern __declspec (dllexport) dynamic_test dynamic_objtest; |
Dynamic_dll.cpp
#include " dynamic_dll.h " #include <STDIO.H> void dynamic_ Test::p rint () { printf ( "Dynamic_ Test::p rint () in dynamic load dll. "); } void static_function () { printf ( "Dynamic_function in dynamic load dll. "); } Dynamic_test dynamic_objtest; |
Configuration type selection: Dynamic library (DLL). and then compile (build) .
Create a project uselib
#include <stdio.h> #include <Windows.h> int Main () { HINSTANCE hDLL = LoadLibraryA ("Dynamic_load.dll"); if (Hdll==null) { FreeLibrary (hDLL); return 0; } typedef void (_stdcall*func) (); Func F; f = (func) GetProcAddress (hDLL,"dynamic_function"); if (f) (*f) (); GetChar (); } |
Note that we must pass in the exact path of the DLL in the LoadLibraryA, and if you use char* 's path, use LoadLibraryA, otherwise Unicode uses Loadlibraryw. The dynamic import class is a bit of a hassle, so let's not talk about it.
Creation and use of Windows libraries: static libraries + dynamic libraries