Differences between creating your own DLL and Lib: DLL and Lib files

Source: Internet
Author: User
Tags import database

Address: http://blog.csdn.net/dizuo/article/details/4103614

Third-party libraries are often used. For example, glut, FreeType, SDL, and cegui and OSG.

Although these libraries are open-source, they are often used and are not modified.

Three things are involved: the. H. Lib. dll file.

There are three main modes:

. H +. Lib

. H +. Lib +. dll

. Dll

 

The second is the most common, with fewer of the other two

 

1. Implement a DLL and Lib by yourself:

Http://msdn.microsoft.com/en-us/library/ms235636 (vs.80). aspx

 

Note that my compiler is vs2005.

 

1. Create a Win32 console application, enter the project name, click OK, and click Next. Select application type: DLL additional option: Empty Project
. Click OK

2. Add the header file mathfuncsdll. H to the empty project. The content is as follows:

  1. // Mathfuncsdll. h
  2. Namespace mathfuncs
  3. {
  4. Class mymathfuncs
  5. {
  6. Public:
  7. // Returns a + B
  8. Static _ declspec (dllexport) Double add (double A, double B );
  9. // Returns a-B
  10. Static _ declspec (dllexport) Double subtract (double A, double B );
  11. // Returns a * B
  12. Static _ declspec (dllexport) Double multiply (double A, double B );
  13. // Returns a/B
  14. // Throws dividebyzeroexception if B is 0
  15. Static _ declspec (dllexport) Double divide (double A, double B );
  16. };
  17. }

3. Add the mathfuncsdll. cpp file as follows:

  1. // Mathfuncsdll. cpp
  2. // Compile with:/ehs/ LD
  3. # Include "mathfuncsdll. H"
  4. # Include <stdexcept>
  5. Using namespace STD;
  6. Namespace mathfuncs
  7. {
  8. Double mymathfuncs: add (double A, double B)
  9. {
  10. Return A + B;
  11. }
  12. Double mymathfuncs: Subtract (double A, double B)
  13. {
  14. Return A-B;
  15. }
  16. Double mymathfuncs: multiply (double A, double B)
  17. {
  18. Return a * B;
  19. }
  20. Double mymathfuncs: Divide (double A, double B)
  21. {
  22. If (B = 0)
  23. {
  24. Throw new invalid_argument ("B cannot be zero! ");
  25. }
  26. Return A/B;
  27. }
  28. }

4. Set project properties:

In Solution Explorer on the left, right-click the project name. In the displayed dialog box, click "Left". Set Properties:/General: Right.
Configuration type: select Dynamic library (. dll)

Click Generate:/to generate mathfuncdll.

 

Now you can go to the debug folder and check the mathfuncsdll. dll mathfuncsdll. Lib files that we are most concerned about. Note that the mathfuncsdll. Lib file is only 3 kb in size.

 

2. Use DLL and Lib files

Create an empty Win32 console application: usemathdll project name

Add the CPP file myexecrefsdll. cpp:

  1. # Include <iostream>
  2. # Include "mathfuncsdll. H"
  3. # Pragma comment (Lib, "mathfuncsdll. lib") // import the Lib File
  4. Using namespace STD;
  5. Int main ()
  6. {
  7. Double A = 7.4;
  8. Int B = 99;
  9. Cout <"A + B =" <
  10. Mathfuncs: mymathfuncs: add (a, B) <Endl;
  11. Cout <"a-B =" <
  12. Mathfuncs: mymathfuncs: Subtract (a, B) <Endl;
  13. Cout <"a * B =" <
  14. Mathfuncs: mymathfuncs: multiply (a, B) <Endl;
  15. Cout <"A/B =" <
  16. Mathfuncs: mymathfuncs: Divide (a, B) <Endl;
  17. System ("pause ");
  18. Return 0;
  19. }

Then, put the mathfuncsdll. lib and mathfuncsdll. H files in the same directory as myexecrefsdll. cpp, that is, the current directory. Set mathfuncsdll. dll can be put under system32. Of course, the simplest way is to put it in the debug or release folder of usemathdll. The later generated EXE can find the desired DLL file in the current directory. (Dynamic Link file. If the DLL is not put into system32 or debug, the program cannot run. Because the executable code of the real function is in the DLL. The LIB file is just an index, and the. h file only provides an excuse .)

Now you can click "run:

The result is as follows:

  1. A + B = 106.4
  2. A-B =-91.6
  3. A * B = 732.6
  4. A/B = 0.0747475
  5. Press any key to continue...

 

3. directly use the. h and Lib files

Open the mathfuncsdll project and set the project properties according to entry 1. However, select the static library (. Lib) for the final configuration type)

Then generate mathfuncsdll and go to debug to check whether the mathfuncsdll. Lib file is changed to 29 KB.

Then replace mathfuncsdll. Lib with the original mathfuncsdll. IB file under the usemathdll project directory, and delete mathfuncsdll. dll under debug, so that usemathdll can still run normally.

Below is a detailed comparison of DLL and Lib:

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

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.

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.