Differences between static library lib and dynamic library DLL creation and Examples
1. What is static Connection Library and dynamic link library?
Both static and dynamic link libraries are shared.CodeIf you use a static Link Library, all the commands in lib are directly included in the final generated EXE file, whether you wish or not. However, if a DLL is used, the DLL does not need to be included in the final EXE file. During execution of the EXE file, the DLL file can be dynamically referenced and detached. Another difference between a static link library and a dynamic link library is that the static Link Library cannot contain any other dynamic or static Link Library, other dynamic or static link libraries can be included in the dynamic link library. The Calling rules of the static Link Library and the static Link Library are compared as follows.
For static link libraries (relatively simple ):
First, the developer of the library is required to use the static Link Library to provide the. h header file and. Lib file of the generated library.
The Declaration format in the. h header file of the generated library is as follows:
Extern "C" function return type function name (parameter table );
Before callingProgram. CppSource codeThe file is as follows:
# Include ".. \ Lib. H"
# Pragma comment (Lib, ".. \ debug \ libtest. lib ")
// Specify the link to the static database
Second, because the static Link Library contains all commands into the EXE file generated by the calling program. Therefore, if you use a static link library, you do not need to use it to export a function! Don't even ask for it! :)
For Dynamic Link Libraries:
The developer of the library is required to use the dynamic link library to provide the generated. Lib file and. dll file. Alternatively, you can only provide DLL files.
First, we must first notice that the functions in the DLL are divided into two types:
(1) DLL export function, which can be called by a program;
(2) DLL internal functions can only be used in DLL programs, and applications cannot call them.
Therefore, if you want to call a function in a DLL, you must specify in some form or method which function you want to call.
If the DLL defines a class, you must add a reference to the lib and header files before calling the class. As follows:
# Include ".. \ Lib. H"
# Pragma comment (Lib, ".. \ debug \ libtest. lib ")
2. Example
Example:
Creation process of static Link Library:
For example, we create a custom string class chironstring,
You only need to add the class in the IDE, and then the corresponding function body of the program
The Code is as follows:
Sdll. h file
------------------------------------------------------------------------
// Hironstring. h: interface for the chironstring class.
//
//////////////////////////////////////// //////////////////////////////
# If! Defined (afx_hironstring_hsf-b23c5e5e_0e8b_4030_b057_34a40c934c591_ded _)
# Define afx_hironstring_hsf-b23c5e5e_0e8b_4030_b057_34a40c934c590000ded _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
Class chironstring
{
PRIVATE:
Char * m_data;
Public:
Char * getdata ();
Chironstring (chironstring & other );
Int length ();
Chironstring ();
Chironstring (char * Str );
Chironstring & operator = (chironstring & other );
Virtual ~ Chironstring ();
};
# Endif //! Defined (afx_hironstring_hsf-b23c5e5e_0e8b_4030_b057_34a40c934c591_ded _)
Sdll. cpp is as follows:
--------------------------------------------------------------
// Hironstring. cpp: Implementation of the chironstring class.
//
//////////////////////////////////////// //////////////////////////////
# Include "stdafx. H"
# Include "hironstring. H"
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
Chironstring: chironstring ()
{
M_data = NULL;
}
Chironstring: chironstring (char * Str)
{
Int Len = strlen (STR );
M_data = new char [Len + 1];
Strcpy (m_data, STR );
}
Chironstring ::~ Chironstring ()
{
Delete m_data;
}
Int chironstring: length ()
{
Return strlen (m_data );
}
Chironstring: chironstring (chironstring & other)
{
Int Len = strlen (other. m_data) + 1;
M_data = new char [Len];
Strcpy (m_data, other. m_data );
}
Chironstring & chironstring: Operator = (chironstring & other)
{
If (this = & other)
Return * this;
If (m_data! = NULL)
Delete [] m_data;
Int Len = strlen (other. m_data) + 1;
M_data = new char [Len];
Strcpy (m_data, other. m_data );
Return * this;
}
Char * chironstring: getdata ()
{
Return m_data;
}
Then, compile the program and generate sdll. Lib.
Customer Call: publish chironstring. h and sdll. lib to the client, then the client can call the static link library we have compiled.
Example 2:
Dynamic Link Library Creation
We still create a custom string processing class chironstring. The difference is that it is a dynamic link library DLL.
The export of the dynamic link library needs to write the corresponding macro in the corresponding header file
Mydll. h : You have customized some class (function) Export macros (this file is automatically generated by IDE) as follows:
------------------------------------------------------------------
# ifdef mydll_exports
# define mydll_api _ declspec (dllexport)
# else
# define mydll_api _ declspec (dllimport)
# endif
This is the macro definition of the export class, this macro must be added to the export class before it can be exported.
here mydll_exports will appear in Preprocessor definition on the project --> Settings --> C/C ++ page, this macro indicates that it wants to define an export macro
chironstring. h custom Class header file
--------------------------------------------------------------
// hironstring. h: interface for the chironstring class.
//////////////////////////////// /// //
# If! Defined (afx_hironstring_h0000518e9ec4_0837_4e45_9516_7d6a70cd3d0f0000included _)
# Define afx_hironstring_h0000518e9ec4_0837_4e45_9516_7d6a70cd3d0f0000included _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
# Include "mydll. H"
ClassMydll_apiChironstring // Add mydll_api to indicate that this is an export class
{
PRIVATE:
Char * m_data;
Public:
Char * getdata ();
Chironstring (chironstring & other );
Int length ();
Chironstring ();
Chironstring (char * Str );
Chironstring & operator = (chironstring & other );
Virtual ~ Chironstring ();
};
# Endif //! Defined (afx_hironstring_h0000518e9ec4_0837_4e45_9516_7d6a70cd3d0f0000included _)
Chironstring. cpp
------------------------------------------------------------
// Hironstring. cpp: Implementation of the chironstring class.
//
//////////////////////////////////////// //////////////////////////////
# Include "stdafx. H"
# Include "hironstring. H"
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
Chironstring: chironstring ()
{
M_data = NULL;
}
Chironstring: chironstring (char * Str)
{
Int Len = strlen (STR );
M_data = new char [Len + 1];
Strcpy (m_data, STR );
}
Chironstring ::~ Chironstring ()
{
Delete m_data;
}
Int chironstring: length ()
{
Return strlen (m_data );
}
Chironstring: chironstring (chironstring & other)
{
Int Len = strlen (other. m_data) + 1;
M_data = new char [Len];
Strcpy (m_data, other. m_data );
}
Chironstring & chironstring: Operator = (chironstring & other)
{
If (this = & other)
Return * this;
If (m_data! = NULL)
Delete [] m_data;
Int Len = strlen (other. m_data) + 1;
M_data = new char [Len];
Strcpy (m_data, other. m_data );
Return * this;
}
Char * chironstring: getdata ()
{
Return m_data;
}
After compile, The mydll. dll and mydll. Lib files are generated.
Client call:
Submit mydll. dll, mydll. Lib, and chironstring to the client.
Notes:
To call the function in the DLL, you need to take three steps:
Handle H = loadlibrary (dllname) --> getprocaddress (H, functionname) returns the function pointer and calls its function through the function pointer --> freelibrary (h)
For example, another. dll has an int add (int x, int y) function. The complete call process is as follows:
Typedef int (* funptr) (INT, INT); // defines the function pointer
Funptr;
Handle H = loadlibrary ("another. dll ");
Funptr = (funptr) getprocaddress (H, "add ");
Funptr (2, 3); // 2 + 3;
Freelibrary (h );