Static library lib and dynamic library DLL

Source: Internet
Author: User
Tags export class

Static link of vs2008 lib

I. Static and Dynamic Link Libraries


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.

When you need to call a function, the dynamic library finds the function based on the function ing table and calls it to the stack for execution. If there are multiple calls to the same function in the DLL file in the current project, this function will only be copied when executed. However, if there are multiple calls to the same function in the Lib file, the function will beProgramMultiple copies are left in the execution space, and a copy is generated when one call is made.

The Calling rules of the static Link Library and the static Link Library are compared as follows:

1. Static link library (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 );

In the. cppSource codeThe file is as follows:

# Include ".. \ Lib. H"

# Pragma comment (Lib ,".. \ debug \ libtest. lib ") // specify the link to the static library (or enter the path of the Lib file in the Lib column of the IDE, and enter the directory of the Lib folder in the lib directory column of the IDE)

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!

Personal Understanding: in fact, we can think that static links are equivalent to introducing and compiling the source code of the Lib project to be directly linked, because the actual results should also be equivalent, and eventually they will all be an EXE file, the functions are the same, but there are some additional compilation links.

2. Dynamic Link Library:

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) Export functions, which can be called by programs;

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.

The following methods can be used to export DLL files:

# Ifdef wll_exports

# Define wll_api _ declspec (dllexport)

# Else

# Define wll_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.

Wll_exports will appear in the Preprocessor Definition of project Setting C ++ Preprocessor. This macro indicates that it wants to define an export macro. When the current library is compiled, the classes with the wll_api added will be exported, and other calling DLL or EXE containing the header file will be declared as the imported class because the wll_api macro is not defined.

Ø static links can be used to call dynamic library functions.

The main steps are as follows:

1) include the header file exported from DLL.

2) use # pragma comment (Lib, ".. \ debug \ libtest. lib") to import the *. Lib header file generated by the dynamic library. Or add the Lib file to the additional dependencies of Project à settings à linker à input.

3) Place the *. dll file generated by the dynamic library to the same directory of EXE or DLL.

You can also use dynamic loading for calling.

The steps are as follows:

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 handle = loadlibrary ("another. dll ");

Funptr = (funptr) getprocaddress (handle, "add ");

Funptr (2, 3); // 2 + 3;

Freelibrary (handle); // release the loaded dynamic library

Ii. Lib File

Currently, there are two types of libraries with the Lib Suffix:Static Link Library

(Static libary, hereinafter referred to as "static library"), the other isDynamic connection library (DLL, hereinafter referred to as "dynamic library ")

(Import libary ").

The static library is a package of one or more OBJ files, so someone simply calls the process of generating lib from the OBJ file archive, that is, merging it 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.

A dynamic library usually has a corresponding Import and Export library, which facilitates the program to load the dynamic link library statically. Otherwise, you may need to load the DLL file by yourself, and then manually getprocaddress to obtain the corresponding function. With the import/export function, you only need to link the import/export function and call the function according to the declaration of the header file function interface.

Import-to-database and static databases are very different. They are actually different.The static library itself contains the actual execution code, symbol table, and so on. For the import and export operations, the actual execution code is in the dynamic library, and the import and export operations only contain the address symbol table, make sure that the program finds some basic address information of the corresponding function.

This is actually a common method for many open-source code releases:

1. Pre-compiled Development Kit: contains some. DLL files and some. Lib files. Here,. Lib is imported to the database, instead of being a static database.However, the method of introduction is the same as that of static libraries. You must add these. Lib paths to the link path.

While. dll is better placed in the directory where the generated application EXE execution file is the same. In this way, the dynamic link library is automatically transferred to the runtime.

2. Compile by yourself: the source code is downloaded and compiled by readme. It is likely to generate library files for. dll +. Lib (import to database ).

3. If you only have DLL and you know the function prototype of the DLL, you can directly use loadlibary in your own program to call the DLL file, getprocaddress to obtain the function address, and then call it.

Iii. DLL files

A 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, 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.

The dynamic link differs from the static link in that it allows the executable module (. dll file or. EXE file) to only contain the information required to locate the executable code of the DLL function at runtime. In a static link, the linker obtains all referenced functions from the static Link Library and puts the library together with the code into the executable file.

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.

An API is an application programming interface. It is a group of functions that can be used to operate components, applications, or operating systems. In typical cases, an API is composed of one or more DLL that provides certain special functions.

DLL is a file that contains functions that can be called by any application running in Microsoft Windows. During runtime, the functions in the DLL are dynamically linked to the application that calls it. No matter how many applications call a function in the DLL, only one file on the disk contains the function and the DLL is created only when it is transferred to the memory. The most frequently-heard API may be a Windows API, which includes various DLL that constitute the Windows operating system. Every windows application interacts directly or indirectly with Windows APIs. Windows API ensures that all applications running in Windows behave in the same way.

With the development of the Windows operating system, several versions of Windows APIs have been released. Windows 3.1 uses the Win16 API. Microsoft Windows NT, Windows 95, and Windows 98 use Microsoft Win32 APIs. In addition to Windows APIs, other APIs have also been released. For example, the Mail Application Programming Interface (mapi) is a set of DLL that can be used to write email applications. APIs are traditionally written for C and C ++ programmers who develop Windows applications.Programming Language(Including VBA) You can also call functions in DLL. Because most of the DLL files are mainly written and organized for C and C ++ programmers, the method for calling the DLL function is different from that for calling the VBA function. When using APIs, you must understand how to pass parameters to DLL functions.

Warning calling Windows APIs and other DLL functions may adversely affect your applications. When calling DLL functions directly from your own code, you bypass some of the security mechanisms commonly provided by VBA. If an error occurs when defining or calling a DLL function (which is unavoidable by all programmers), it may cause an application error (also known as a universal protection error or GPF) in the application ). The best solution is to save the project before running the Code and ensure that you understand the principle of DLL function calling.

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.