This section describes the differences between static and dynamic link libraries, and how to create and use static link libraries in VC ++ 6.0.

Source: Internet
Author: User

First, we will introduce the Link Library: The Link Library is divided into two types: Dynamic Link Library and static Link Library.

LIB is a static Link Library. It is a static link during program compilation and connection. Its file format is. lib. That is, when the program uses a static link library ,. functions in the lib file are linked to the final executable file, because all the content required by the application is copied from the library, therefore, static libraries do not need to be released together with executable files.

DLL is a dynamic link library that is called when the program is running. Its file format is. dll. That is, when the program uses dynamic links, the functions in the. dll file are not linked to the executable file. The executable file only saves the address information of the function. However, the dynamic library must be released with the application.

The following describes how to use dynamic and static libraries in VC6.0.

First, we will explain how to create and use static databases.

First, create a Win32 Console Static Library project named Max_Func.

Add a header file named Max_Func.h to the project.

The Code is as follows:

Int max (int a, int B );

Add the source file named Max_Func.cpp to the project.

The Code is as follows:

# Include <iostream. h>
# Include "Max_Func.h" // declare and define the max Function
# Include "Macro_Define.h" // The following three header files are subsequently defined
# Include "Global_Value.h"
# Include "Extern_Value.h"

Int max (int a, int B)
{
Cout <"COPYRIGHT" <endl;
++ GLOBAL_NUM;
Cout <"Call Max_Func" <GLOBAL_NUM <endl;
Return a> B? A: B;
}

Add the header file Macro_Define.h to the project.

The Code is as follows:

# Define COPYRIGHT "Scripted by WP ."

Add the header file Global_Value.h to the project.

The Code is as follows:

Int GLOBAL_NUM = 0;

Add the header file Extern_Value.h to the Project

The Code is as follows:

Extern int GLOBAL_NUM;

Compile the above project file to obtain the Max_Func.lib file.

The files for external development are Max_Func.lib and Max_Func.h. Macro_Define.h, Global_Value.h, and Extern_Value.h if they are under project development.

How to add it to the project? The following describes several methods. Use a method to demonstrate this.


The first method to add is # pragma comment (lib, "D: \ MFC \ Test_Project_4 \ lib \ Max_Func.lib.

"D: \ MFC \ Test_Project_4 \ lib \ Max_Func.lib" is the absolute path of the Max_Func.lib file. You can also use relative paths.

Then add # include "Macro_Define.h" to the function file in. lib.


The second method is to directly add the Max_Func.lib file to the project to be used. Select Add Files to Project... on the Project.

Then add # include "Macro_Define.h" to the function file in. lib.


The third method is to add the search path of the. lib file to the Library Files of Tools/Options in VC6.0. And in the Project/Setting Link label

Add the static library name to object/library modules.

Then add # include "Macro_Define.h" to the function file in. lib.


The following program uses the first method

# Include <iostream. h>
# Include "Macro_Define.h"
# Include "Extern_Value.h"
# Include "Max_Func.h"
# Pragma comment (lib, "D :\\ MFC \ Test_Project_4 \ lib \ Max_Func.lib") // Add a static library
Void main (void)
{
Cout <COPYRIGHT <endl;
Cout <max (19,49) <endl;
Cout <max (200,100) <endl;
}

The above process can call functions in the static library.


If you have any questions, leave a message below.


The following describes how to create and use a dynamic library.

The project name for creating a dynamic link library is Max_Func.

Add a header file named Max_Func.h to the project.

The Code is as follows:

Int max (int a, int B );

Add the source file named Max_Func.cpp to the project.

The Code is as follows:

# Include <iostream. h>
# Include "Max_Func.h" // declare and define the max Function
# Include "Macro_Define.h" // The following three header files are subsequently defined
# Include "Global_Value.h"
# Include "Extern_Value.h"

Int max (int a, int B)
{
Cout <"COPYRIGHT" <endl;
++ GLOBAL_NUM;
Cout <"Call Max_Func" <GLOBAL_NUM <endl;
Return a> B? A: B;
}

Add the header file Macro_Define.h to the project.

The Code is as follows:

# Define COPYRIGHT "Scripted by WP ."

Add the header file Global_Value.h to the project.

The Code is as follows:

Int GLOBAL_NUM = 0;

Add the header file Extern_Value.h to the Project

The Code is as follows:

Extern int GLOBAL_NUM;

Compile the above Code to generate the file Max_Func.dll

The following also generates a Max_Func.lib file for exporting DLL files.

Max_Func.def needs to be created to join the project

The content is as follows:

LIBRARY Max_Func_1 // specifies the name of the dynamic LIBRARY to be exported

EXPORTS max // export Function

The method for adding DLL files is almost the same as that of LIB, because all files in the lib format are added to the project. Note that you need to add the DLL file to the project directory or where it can be searched. Call the functions in the dll file during program running. Find the project in dll through the function address

The function to be called.


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.