H header file. Lib library file. dll dynamic link library file relationship

Source: Internet
Author: User
Tags import database

. H header files are required for compilation, Lib is required for linking, and DLL is required for running.

The dependency. Lib is not. dll. If DLL is generated, the Lib file is also generated. If you need to complete source code compilation and linking, it is enough to have header files and Lib. If dynamic connection is also enabled, DLL is enough. In the development and debugging stages, it is best to have both.

. H. Lib. dll:

The role of the H file is to declare the function interface.

The role of the DLL file is: function executable code

When we reference a function in an H file in our program, how does the leader know which DLL file to call? This is the role of the Lib file: Tell the linker which dll The called function is in and where the function Execution Code is in the DLL. That is why dependencies need to be attached. lib file, which serves as a bridge. If a static library file is generated, there is no DLL and only Lib. the executable code of the function is also in the Lib file.

Currently, there are two types of libraries with the Lib Suffix: static libary (hereinafter referred to as "static library") and dynamic connection library (DLL, import libary (hereinafter referred to as "Import Database "). 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.

Generally, dynamic library programs include lib files and DLL files. The LIB file must be connected to the application during the compilation period, and the DLL file will be called only during the runtime. If there is a DLL file, the corresponding lib file is generally some index information, the specific implementation is in the DLL file. If only the Lib file is available, the Lib file is statically compiled and the indexes and implementations are all included. The advantage of static compiling lib files is that dynamic libraries are no longer needed during installation. However, there are also some disadvantages, that is, the application is relatively large and the flexibility of the dynamic library is lost. During version upgrade, new applications must be released at the same time. In the case of a dynamic library, there are two files, and one is imported into the database (. lib) file. One is a DLL file. The imported file contains the name and location of the function exported by the DLL. The dll contains the actual function and data, the application uses the Lib file to link to the required DLL file. The functions and data in the library are not copied to the executable file. Therefore, in the executable file of the application, stores not the called function code, but the memory address of the function to be called in the DLL, in this way, when one or more applications are running, the program code is linked to the called function code, thus saving memory resources. From the above description, we can see that the DLL and. Lib files must be released along with the application, otherwise the application will produce errors.

Methods for loading lib files in VC:

① Add the Lib file directly to the project file list

Open the file view page in VC, select the project name, right-click the project, and select the "add files to project" menu, in the pop-up file dialog box, select the Lib file to be added to the DLL.

② Set project settings of the project to load the DLL lib File

Open the Project Settings menu of the project, select link, and enter the DLL lib file in the text box under object/library modules.

③ Program code

Add the pre-compiled command # pragma comment (Lib, "*. lib"). The advantage of this method is that the conditional pre-compiled command can be used to link lib files of different versions. In the debug mode, the generated lib file is the debug version, such as regd. Lib. In the release mode, the generated lib file is the release version, such as Regr. Lib.

After the application loads the Lib file of the DLL, it also needs to load the header file (*. h) include it. In this header file, the prototype of the function defined in DLL is provided, and then declared.

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.