Dynamic Link Library, static Link Library, and input/output Operator Overloading of Classes

Source: Internet
Author: User
Tags export class

Let's talk about the files available after the Link Library Project is generated. The dynamic link library project generates a. dll file and A. Lib file. The static Link Library project has only one. Lib file. Of course, to use a dynamic or static link library, you must provide the header file of the library. Some people call the. Lib file generated by the dynamic library project a static link library. This is incorrect. It is not a static Link Library, but a dynamic link library, that is, the. dll file is imported to the database. Although both static and dynamic libraries are. Lib files, they are actually different. The static library itself contains the actual execution code, address symbol table, and so on. For the import and export operations, the actual execution code is in the dynamic library, the imported database only contains the address symbol table. Make sure that the program finds some basic address information of the corresponding function. In fact, the Export and Import functions in the. Lib file are jump commands corresponding to the corresponding location in the DLL file to perform DLL jump when calling the DLL function when executing an external program. Generally, the import/export file. LIB contains the same name as the corresponding DLL file, and an ordered table that specifies the DLL output function entry.

 

What about Dynamic and Static link libraries? Static and dynamic libraries share code, all commands in lib are directly included in the final generated EXE file. However, if you use a DLL (that is, a dynamic link library), the DLL does not have to be included in the final EXE file, when executing an EXE file, you can dynamically reference and uninstall the DLL file that is independent from the EXE file. 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 use of dynamic and static link libraries is different in that it allows executable modules (. DLL file or. EXE file) only contains the information required to locate the executable code of the DLL function at runtime. In the use of the static Link Library, the linker obtains all referenced functions from the static Link Library and puts the library together with the code into the executable file.

First, you must understand what a static link library is. A static Link Library is a package of one or more OBJ files. Therefore, someone simply calls the process of generating lib from the OBJ file archive, that is, it is merged together. For example, if you link a static library, if there is a mistake in it, it will accurately find which obj is wrong, that is, static Lib is only a shell. When our application project uses a static Link Library, the static link library should participate in compilation. During the link process before the execution file is generated, link all commands of the static link library directly to the executable file. Therefore, after the executable file is generated, the static Link Library is connected. lib files can be discarded.

Dynamic link library (DLL) is an executable file used as a shared function library. Dynamic links provide a way for a process to call a function that does not belong to its executable code. The executable code of a function is located in a. dll file, which contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLL also helps to share data and resources. Multiple applications can simultaneously access the content of a single DLL copy in the memory. Using Dynamic Links instead of static links has several advantages. DLL saves memory, reduces swap operations, saves disk space, makes it easier to upgrade, provides after-sales support, and extends the MFC Library Class mechanism to support multi-language programs, and make it easy to create international versions.

 

Understand the differences between dynamic and static link libraries. Let's take a look at their creation and use. First, create a static Link Library Project staticlib, and add staticlib to it. H and staticlib. CPP, in staticlib. declare the following function in H: extern "C" int add (int x, int y); // declare an external function as a C-compiled, connection method, in staticlib. in CPP, perform the following implementation: int add (int x, int y) {return X + Y;} compile the project and we can get a staticlib. lib file. For the use of the static Link Library, create a testdllslib project. Step 1: Add statilib to the Project Properties> C/C ++> additional include directory. step 2: Add staticlib to the Project Properties> connector> General> additional library directory. path of the Lib file. Step 3: Go to Project Properties> connector> General> input> additional dependency to add the static library name, namely staticlib. lib. Then we can use the Add function in the testdllslib test project, first containing the static library header file # include "staticlib. H ", and then you can directly use the Add function. For example, in the main function, you can directly use int sum = add (2, 3 );. of course, we can actually use another method, that is, to make the following declaration below the header file contained in the source code file that calls the function: # pragma comment (Lib, "staticlib. in this way, we can omit Step 2 and Step 3. Of course, the Lib file in the brackets must ensure the correctness of the path. now, the creation, generation, and use of the static Link Library are complete. It should be mentioned here. Generally, the most important problem about lib files is the occurrence of unresolved symble errors. This is the Lib file connection error or the. C and. cpp files are not included in the project. Especially if the Lib file written in C language is used in the C ++ project, it must include:

 

In view of the current knowledge of the author, the export class in the static library is still being organized..

 

Next let's take a look at the dynamic link library. Create a dynamic link library (. dll) Project mytestdll. Here is a simple example. Add a class to the dynamic link library project. The Class header file content is as follows: hironstring. h:
# Pragma once
# Include <iostream>
# Include <iomanip>

# Include "explort. H"
Using namespace STD;
Class my_dll chironstring

{
Public:
Chironstring (void );
Chironstring (char * Str );

Friend istream & operator> (istream &, chironstring &);
Friend ostream & operator <(ostream &, const chironstring &);
Chironstring & operator = (chironstring & other );
Char * getdata ();
Chironstring (chironstring & other );
Int length ();
Public:
Virtual ~ Chironstring (void );
PRIVATE:
Char * m_data;
}; The header file content hironstring. cpp is as follows:

# Include "stdafx. H"
# Include <windows. h>
# Include "hironstring. H"
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;
}

The exported file content is as follows: explort. h

# Ifdef mydll_exports
# Define my_dll _ declspec (dllexport)
# Else
# Define my_dll _ 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.
Mydll_exports will appear in Preprocessor definition on Properties> Configuration Properties> C/C ++. This macro indicates that it wants to define an export macro.

Then compile the file, we can get a dynamic link library mytestdll. dll file, a dynamic link library imported into mytestdll. Lib. Then let's take a look at how to use the dynamic link library. Because the dynamic link library has an additional import/export file. Lib, we have two ways to use the dynamic link library.

The first is implicit loading: the partitions should be put together. Because the program needs to load the entire DLL at the beginning of running, it cannot start running if it cannot be loaded. Let's take a look at the usage in the test project:

# Include "stdafx. H"
# Include "hironstring. H"
# Include "staticlib. H"

 

 

Another point is that the entire DLL is loaded, which consumes a lot of resources. This is a drawback of implicit loading of dynamic link libraries. However, we can also see that we can directly use the member function class of the export class. The second method is to display the load. I believe most people who use the dynamic link library know how to use it, three steps: 1. Handle H = loadlibrary (dllname) dynamically load the dynamic link library; 2. getprocaddress (H. functionname) returns the function pointer. The function pointer is used to call functions in the dynamic library. 3. freelibrary (H) is used to release the dynamic library handle. 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. However, the class method cannot be called.Examples of dynamic link library display loading have not yet been written.

 

 

Finally, let's take a look at the overload of the input and output operators:

To enable the string class in the DLL example project to output and input, you want to overload the input and output operators. Because the leftmost operand is ostream, it is natural that the member functions of the class cannot be reloaded. Instead, it can only be reloaded using the Friends functions of the class. Therefore, two friends declarations are available in the header file. Then, implement the following in the CPP file,

Istream & operator> (istream & Io, chironstring & S)
{
Const int limit_string_size = 4096;
Char inbuff [limit_string_size];
Io> SETW (limit_string_size)> inbuff;
Chironstring TMP (inbuff );
S = TMP;
Return IO;
}
Ostream & operator <(ostream & OS, chironstring & S)
{
OS <S. getdata ();
Return OS;
}

After compilation, the DLL and Lib files are generated and called by using the implicit Loading Method of the dynamic link library. An error occurs during compilation and an operator Overload error is reported. As a result, the declaration and implementation of the template class must be placed in the same position, that is, either in. h or in. cpp. So I put the implementation of these two overload functions into hironstring. H, compiled the DLL and Lib files, and called them in the test project.

Int _ tmain (INT argc, _ tchar * argv [])
{
Chironstring str1 ("1234 ");
Chironstring str2 = str1;
Int Len = str1.length ();

STD: cout <str1 <STD: Endl; // print out 1234
STD: cout <str2 <STD: Endl; // print out 1234

Int sum = Add2 (2, 3 );

Return 0;
}, So we can extract the following key points:

Key Point 1. Reload the input and output operators, which should be used as friend functions of the class.

Key 2. The returned value should be an input/output stream reference (istream & or ostream &).

Key Aspect 3. The first parameter should be an input/output stream reference (istream & or ostream &).

Key 4. The second parameter should be an object reference of this class. The output stream must also be a constant. References are used to avoid overhead caused by implicit values.

Key 5: Define the overload in the Class header file

Int _ tmain (INT argc, _ tchar * argv [])
{
Chironstring str1 ("1234 ");
Chironstring str2 = str1;

Int Len = str1.length ();

Int sum = Add2 (2, 3 );

Return 0;
}

 

Ferry data: http://hi.baidu.com/zhleilei/blog/item/1ba98c4502ce2b388794739d.html

Http://www.cnblogs.com/winston/archive/2008/07/05/1236273.html

Http://www.cnblogs.com/chio/archive/2008/08/05/1261296.html

Http://blog.csdn.net/huozi19820418/archive/2008/06/19/2565911.aspx

Http://topic.csdn.net/t/20051005/20/4308418.html

 

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.