. 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 want to completeSource codeCompilation and link. It is enough to have a header file and Lib. If dynamic connection is also enabledProgramIt is enough to run a DLL. 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.

DLL files are used to execute functions.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.

Bytes -------------------------------------------------------------------------------------

Differences between static library (LIB) and dynamic library (DLL)

Static Connection Library is to directly link the function code used in the (LIB) file to the target program. Other library files are no longer needed when the program is running; dynamic Link is to link the file module (DLL) where the called function is located and the location of the called function in the file to the target program. When the program is running, find the corresponding function code from the DLL, therefore, support for the corresponding DLL files is required.

Static and dynamic libraries share code, all commands in lib are directly included in the final generated EXE file. 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.

"Each lib file is defined by several functions (assuming only functions"
There are two types of LIB libraries: one is to include information about the DLL file where the function is located and the location of the function in the file, known as export and warehouse; the other is to include the Function Code itself, generally the existing DLL, the former database is used; the former TC/BC in DOS is the latter one. The header file (. h) contains the function prototype declaration ).

"After the # include header file containing these function declarations, our application can use the functions in the Lib file"

You also need to specify the compiler to link the corresponding library file. In the IDE environment, generally all the library files used are specified at a time, and the compiler looks for the libraries required by each module. In the command line compiling environment, the library called by each module needs to be specified.

"What is the difference between the file that directly gives the function definition, such as The. cpp file, and the header file? What is the use of the static link library"
The CPP file is the source code, and the library file is the compiled Binary Code. For example, you can call the Windows API but cannot see the same source code.

"What I still don't understand is that as long as the Lib file in the static link library is used, the content of the entire lib file is put into the EXE file, is it compiled or linked?"
Link lib to the target code.

Static link library (LIB)
In VC ++ 6.0, a new static library project named libtest is created,

Create the Lib. h and Lib. cpp files. The source code of Lib. h and Lib. cpp is as follows:

// File: Lib. h
# Ifndef lib_h
# Define lib_h
Extern "C" int add (int x, int y); // declared as an external function in C compilation and Connection Mode
# Endif

// File: Lib. cpp
# Include "Lib. H"
Int add (int x, int y)
{
Return X + Y;
}

Compile the project to obtain a. Lib file, which is a function library and provides the Add function. After the header file and the. Lib file are submitted to the user, the user can directly use the Add function.

The C library functions (scanf, printf, memcpy, strcpy) in the standard Turbo c2.0 come from this static library.

Next let's take a look at how to use this library and create a new libcall project in the workspace where the libtest project is located. The libcall project contains only one main. cpp file, which demonstrates the call method of the static Link Library. Its source code is as follows:

# Include <stdio. h>
# Include ".. \ Lib. H" // cannot be lost
# Pragma comment (Lib, ".. \ debug \ libtest. lib") // specify the link to the static library
Int main (INT argc, char * argv [])
{
Printf ("2 + 3 = % d", add (2, 3 ));
}
The call to the static Link Library is so simple that we may be using it every day, but we do not understand this concept. # Pragma comment (Lib, ".. \ debug \ libtest. lib") in the Code indicates that the. OBJ file generated in this file should be connected with libtest. Lib.

Bytes -------------------------------------------------------------------------------------------

Use VC ++ to generate static library files
I did nothing to do today. I wrote a little note myself. I don't know if it is useless for a newbie, so I don't have to read it. As a newbie, I am afraid to make a note, it is the encapsulation process of static library files. It is written in VC ++ 6.0. The following is the body. Maybe my language is not professional.

In the past, when we wrote C/C ++ source files, we first compiled each written source file, and compiled the machine code of the target file, that is. OBJ file. (The extension of the target file is not necessarily. OBJ file ).

The standard C/C ++ function machine code we call is actually encapsulated in the Standard C/C ++ static library file, that is, those files with the extension. Lib.

Finally, the linker connects the machine code and static library in each of the compiled target files (the serial machine code in the Standard C/csf-library is linked together to form an executable file module with the extension name ".exe.

Here we will describe how to compile the C/C ++ source file into a static library file, but it is not an executable module and contains executable machine code.

The static library file is like a repository or container, which encapsulates some executable machine codes. These machine codes are the machine codes generated after compiling in the C/C ++ source file.

I. The following describes how to compile and link the C/C ++ source file into a static library file,

In VC ++ 6.0 select File-New-Win32 static library, write the project name to create a workspace, and then select new-file in the menu to add C or C ++ source files for the project.

Assume that we have added a source file named lib_c.c and lib_cpp.cpp to this project.

// Content in lib_c.c

Extern int add (int x, int y) // This function is an external function that can be accessed by any file.

{
Return X + Y;

}

Extern int data_c
// This is an external global variable, which can be accessed by any file

// Content in lib_cpp.cpp

Extern "C" int
Reduce (int x, int y) // Add "C" here to allow the C source file to access the C ++ function code.

{
Return x-y;

}

Extern "C" int data_cpp = 2;

Note the following points:

(1) When the "extern" keyword is modified in the definition of a function or global variable, it indicates that this function or global variable can be accessed by any file, and the "extern" keyword can be omitted without writing, the default value is "extern"

When the "extern" keyword is modified in the function declaration or global variable declaration, it indicates that the current file can only reference the function or global variable modified with the "extern" keyword.

(2) When the "static" keyword is modified in the definition of a function or global variable, this indicates that the function or global variable can only be referenced by the function declaration or global variable declaration modified by the "static" keyword in this file.

When the "static" keyword is modified in the function declaration or global variable declaration, it indicates that the current file can only reference the function or global variable modified with the "static" keyword.

(3) Add "c" to the functions and global variable definitions of the CPP source file to allow the C source file to access the function and global variables. if it is a C ++ source file to access them, it can be added or not added. note that "C" should be capitalized.

Next, compile and link the written C/C ++ source file, and generate a file extension. lib file. this file is a static library file and cannot be run directly, the machine code of the compiled C/C ++ source file has been encapsulated in the static library file created with VC ++ 6.0.

Ii. How to use compiled static library files like the C/C ++ standard library will be discussed below

1. create a project named test with VC ++ 6.0 and add a project named test. c source file to this project, because we will test it, link the machine code of the function or global variable in the library file we compiled to our test. in the C source file, assume that the library file we generated is named test. lib, first copy the following sample code to test. c Medium

// Test. c

# Include <stdio. h>

Extern int
Add (int x, int y); // The current file can only access the Add function defined by the "extern" keyword.

Extern int
Reduce (int x, int y); // The current file can only access the reduce function defined by the keyword "extern ".

# Pragma comment (Lib, "test. lib") // indicates the linker to find the library file in the file path indicated by the string

Int main ()

{
Printf ("% d \ n", add (2, 3 ));
Printf ("% d \ n", reduce (3, 2 ));
Return 0;

}

Here we need to declare the declaration of known functions or global variables in the static library.

# Pragma comment (Lib, "test. lib ") This Command tells the linker to go down to the path indicated by the string to find the library file. Here I put the library file under the current project directory. you can also leave this sentence empty.

You can also choose tools, options, directories, and library files menus or options in VC ++ 6.0, fill in the path of the library file (only the directory path where the key library file is located, but not the library file name). This only tells the path of the directory where the library file is located and does not tell the library file name, in VC ++ 6.0, select "Project-settings-link" and enter the library file name in "Object/library modules ".

2. when the link between the target file of the C ++ source file and the code of the library file is slightly changed, it will not be a waste of words. Suppose we have created a new project and added a project named test. CPP source file, copy the following sample code to test. CPP

// Test. cpp

# Include <stdio. h>

Extern "C" int
Add (int x, int y); // indicates that the C function code is referenced.

Extern int
Reduce (int x, int y );

# Pragma comment (Lib, "test. lib ")

Int main ()

{
Printf ("% d \ n", add (2, 3 ));
Printf ("% d \ n", reduce (3, 2 ));
Return 0;

}

The C function code referenced in the C ++ source file must also be added with "C". However, the C ++ Function Code referenced in the C source file cannot be added with "C ++ ", an error is reported during compilation. You can only add "c" to the Function Definition of the C ++ file ".

Only C ++ supports this reference method. It may be because only C ++ is compatible with C and C is not compatible with C ++.

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.