Differences and links between lib and DLL files

Source: Internet
Author: User

Lib and DLL
Differences and links between lib and DLL files

. Dll is in yourProgramThe file that is connected during running, so it is a relatively small executable file format ,. DLL has other file formats such. OCX, etc. All. all DLL files are executable.

. Lib is the file that is connected when your program compiles the connection. Therefore, you must inform the compiler of where the Lib file is connected. In general, compared with dynamic connection files, lib files are also called static connection libraries. When youCodeWhen files are compiled into these formats, they cannot be changed later. To use the Lib file, you must:
1. Include a corresponding header file to inform the compiler of the specific content in the Lib file.
2. Set the Lib file to allow the compiler to find the compiled binary code.

If you want to separate a DLL file from your code to replace the static Connection Library, you still need a lib file. This lib file will be connected to a program to tell the operating system what DLL file you want to use during running. Generally, the LIB file contains the corresponding DLL file name and a sequence table that specifies the DLL output function entry. If you do not want to use the Lib file or do not have the Lib file, you can use the Win32 API functions loadlibrary and getprocaddress. In fact, we can open the Lib file in binary format in Visual C ++ ide. In most cases, we will see the ASCII code type C ++ function or some function names for heavy-load operations.

In general, the most important trouble with LIB files is the occurrence of unresolved symble errors, which are the Lib file connection errors or are not included. C ,. if a CPP file is written in C Language lib file in the C ++ project, it must include:
Extern "C"
{
# Include "myheader. H"
}
This is because the Lib file written in C language is not damaged by the name required by C ++, and the c function cannot be overloaded. Therefore, the connector will fail.

====================================

How to Create a DLL without using MFC in VC

Method 1: Use Export and Import

Create a console application in VC and create two files: DLL. h and DLL. cpp.

DLL. h

# Ifdef mylibapi
# Else
# Define mylibapi extern "C" _ declspec (dllimport)
# End if

Mylibapi int add (INT ileft, int iright)
Mylibapi int sub (INT ileft, int iright)

DLL. cpp

# Define mylibapi extern "C" _ declspec (dllexport)

# Include "DLL. H"

Int add (INT ileft, int iright)
{
Return ileft + iright;
}

Int sub (INT ileft, int iright)
{
Return ileft-iright;
}

Save the file.
Add "/DLL" at the bottom of the project-> setting-> link, and be sure to match the previous item before "/".
There are spaces.
After compilation, you can find the DLL and Lib files under debug or release.
Contains DLL. H files.

Method 2: Use the def File
Create a console application and two files DLL. h and DLL. cpp.

DLL. h

Int add (INT ileft, int iright );
Int sub (INT ileft, int iright );

DLL. cpp

# Include "DLL. H"

Int add (INT ileft, int iright)
{
Return ileft + iright;
}

Int sub (INT ileft, int iright)
{
Return ileft-iright;
}

Create a. Def file under the current directory. The file name should be the same as the DLL name to be output.
Is. Def, which is written as follows:

Library dllname. dll
Exports
Add @ 1
Add @ 2
Then add the file to the project,
Set/DLL in link, and then compile
You can find the DLL and Lib in debug or release.
Add the DLL. h file when using it.

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.