Creation and use of Windows static libraries and dynamic libraries

Source: Internet
Author: User

In actual programming development, we often encounter a lib file that cannot be found when the runtime cannot find a DLL file or link. Then, we began to mess with Google, and then change the VS2005 settings, maybe ok, we have others developed DLL or LIB into our programming, then what are these lib,dll? Below, I would like to take a thin road.

First, we say the first : static link library (libary)

We use VS2005 to make a static link library first

Open VS2005, new à project (STATICCAI) ÀWIN32 Console Application

Create a new static_lib.h and static_lib.cpp two files, the contents of these two files are as follows:

Static_lib.h :

int add (int x,int y);

int substract (int x, int y);

Static_lib.cpp :

#include "Static_lib.h"

int add (int x,int y)

{

return x + y;

}

int substract (int x,int y)

{

return x-y;

}

Then compile, build the solution, OK, so no accident will be in the Debug folder (and Staticcai side-by-side) to generate a staticCai.lib file, OK, this is our good static link library. Below, let's see how to use this static link library. We create a new Win32 console program, and the new main.cpp content is as follows:

#include <iostream>

#include "Static_lib.h"

#pragma comment (lib, "Static.lib")

using namespace Std;

int main ()

{

cout << Add (3, 4) << Endl;

cout << substract (5, 3) << Endl;

return 0;

}

and copy the StaticCai.lib and static_lib.h two files to a folder that is tied to main.cpp. Then, we compile, link, execute the program, and we get the results.

#pragma comment (lib, "Static.lib") This sentence is the same as the effect of adding a dependency on the Project à property à connector à. At this point, how to do the static link library and how to use the static link library is done. Now, we have just copied over the StaticCai.lib to delete, we found that the program is still executed, but can no longer link. So, we come to the conclusion that we need a static link library when we re-link, once the link is successful, the executable file is generated, then the static link library is no longer needed.

Second , we say the second one : dynamic link library (libary)

Again, let's do a dynamic link library, as in the above steps, first build the project, only the last step slightly different

Then, create a new Dll.cpp file (we won't do the. h file here) and click on the contents:

#define DLL_API _declspec (dllexport)

#include <iostream>

using namespace Std;

DLL_API int Add (int a,int b)//implement two integers to add

{

return a+b;

}

DLL_API int subtract (int a,int b)//implement two integer subtraction

{

return a-B;

}

Then, we compile and build the solution, and the DllCai.dll and DllCai.lib are generated under the Debug folder. Well, at this point, the dynamic link library is done, below we see how to use, create a new Win32 console program, the new main.cpp content is as follows:

#include <iostream>

using namespace Std;

#pragma comment (lib, "DLL.lib")

extern int Add (int a,int b);

extern int subtract (int a,int b);

int main ()

{

cout << Add (3, 4) << Endl;

cout << Subtract (5, 3) << Endl;

return 0;

}

Then copy the DllCai.dll and dllCai.lib to the directory that is tied to the main.cpp. Next, compile, link, execute, and you'll see the same results as the last one. Then, we have dllCai.lib to delete, the program is still executed, but can not be linked, and then, we have to delete the DllCai.dll, the program can be compiled, link, but the implementation of the time is yellow

Therefore, we say: For the dynamic link library, the link is required when the. lib file, run when the. dll file is required.

At this point, the static link library and the dynamic link library We are finished, we do the contrast and add:

1, two lib files

We found that whether it is a static link library or a dynamic link library, and finally there are LIB files, then what is the difference between the two? In fact, the two are completely different things. StaticCai.lib size of 4kb,dllcai.lib size is 2KB, static library corresponding lib file called Static Library, dynamic library corresponding lib file is called import library. In fact, the static library itself contains the actual execution code, symbol table and so on, and for the import library, its actual execution code is located in the dynamic library, the import library contains only the address symbol table, etc., to ensure that the program to find some basic address information of the corresponding function.

2, for the static link library, we compile and link all the code has been imported in, so when the executable file is generated, the executable file contains all the code. Therefore, the static library is no longer needed when the executable is run. This is also why we delete the StaticCai.lib program to do so, and for the dynamic link library, in fact, the executable file does not contain the contents of the DLL, but only through the import library (. lib) know the corresponding address information, so the executable file at runtime to load the DLL dynamically, which is why we delete Dllcai The program cannot execute after the. dll.

3, for the DLL, we can not lib file.

If you do not lib file, we can use the function pointer to achieve our purpose:

#define DLL_API _declspec (dllexport)

#include <iostream>

using namespace Std; Notice the extern "C" here, which must be added

extern "C" Dll_api int Add (int a,int b)//implement two integers to add

{

return a+b;

}

extern "C" Dll_api int subtract (int a,int b)//implements two integer subtraction

{

return a-B;

}

#include <iostream>

#include <Windows.h>

using namespace Std;

typedef int (*FUNC) (int x, int y); function pointers

int main ()

{

HINSTANCE hinstance = LoadLibrary ("DLL.dll");

if (hinstance = = NULL)

{

cout << "SB" << Endl;

return 0;

}

Func add = (func) GetProcAddress (hinstance, "add");

Func sub = (func) GetProcAddress (hinstance, "subtract");

cout << (*add) (3,4) << Endl;

cout << (*sub) (5,3) << Endl;

}

Obviously, this method does not use Lib file convenient, if in order to call a function and then to get a function pointer, how much trouble ah, so, we in the actual development, with the numerous third-party extension library, others are provided:

. h file (class, Declaration of function)

. dll files (implementations of classes or functions)

. lib file (import library)

Summary:

First, Static library

* Static libraries are compiled in a binary file with a. lib extension for the functions that are required to run the program. When the program link, the binary data in the static library and other data in the program are put together. The program runs without the support of Lib and DLL files. The disadvantage of this is that the development of the program takes up a large amount of disk space. In particular, there is no need for a function in a Windows system that would otherwise be required to run a program, and use its own static library every time the program is developed.

* Static libraries exist as. lib files

* The executable file generated after the link contains the code for all functions that need to be called, so it takes up disk space

* If there are multiple processes (calling the same library function) running in the middle of memory, there are multiple copies of the same library function code in memory, thus taking up more memory space.

Second, dynamic library

* Dynamic libraries are developed only by putting the function names and parameters in the DLL into the application, which is run by calling functions in the DLL based on the function name and parameters, so that applications in the operating system can use the same DLL at the same time. Can effectively save hard disk space, of course, this makes the program design more layered. It also facilitates the division of labor and Information security of software engineers.

* The dynamic library exists as a. dl file and generally has a corresponding introduction library in the form of a. lib file. A resource-only DLL does not generate. lib for inbound.

> introduced libraries and static libraries are *.lib, but the introduction of libraries contains only some function names and parameter information, there is no functional body, is to invoke the dynamic library service, and its relationship with the dynamic library is equivalent to the relationship between the. h file and the. cpp file;

* Two kinds of binding methods of dynamic Library

> Static binding blnding A program that uses static bindings to load memory at first, the loader calculates and determines the address of the dynamic code that the program calls to. This way, the program has a long initialization time, but when the dynamic loading is completed, the program runs fast.

2 dynamic binding a program that uses this method does not complete a dynamic link in the first place, but until the dynamic library code is actually called, the loader calculates the logical address of the dynamic code (the part that was called), and then waits for some time when the program needs to invoke another block of dynamic code , loading the program to calculate the logical address of this part of the code. Therefore, this way the nephew program initialization time is short, but the performance during the run compared to statically bound programs.

* Two methods for using dynamic libraries (Windows)

> method One: Load-time dynamic linking
Include the DLL's input library file (import Library,.lib file) when you want to invoke the DLL's application link. The specific practice is to add an # include at the beginning of the source file, and then you can call the output file in the Dlldemo.dll in the source file.

> method Two: Run-time dynamic linking
Instead of including the input library file at link time, use LoadLibrary or LoadLibraryEx to load the DLL dynamically in the source program.

Creation and use of Windows static libraries and dynamic libraries

Related Article

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.