[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Dynamic Link Library is not a unique feature of Linux. It also exists in Windows. In general, the dynamic Connection Library in Windows ends with *. dll, and the dynamic Connection Library in Linux ends with *. So. Compared with the static Link Library, the dynamic connection library can share memory resources, which can reduce memory consumption. In addition, the dynamic connection can be found by common execution files only after the help of the operating system loader. Therefore, the dynamic connection library can reduce the number of links. With this feature, it is not difficult to find out why many software patches are actually released using dynamic libraries.
So how is the dynamic library generated on Linux?
#include "test.h"int add(int a, int b){ return a + b;}
Header file format,
#ifndef _TEST_H#define _TEST_Hint add(int a, int b);#endif
At this point, if we want to generate a dynamic library, the job is actually very simple. Just enter gcc-shared-FPIC-O libtest. So test. C. Press enter and enter ls to find the libtest. So file under the current directory.
#include <stdio.h>#include "test.h"int main(){ printf("%d\n", add(2, 3)); return 1;}
In the above Code, we found that the Add function is used. How can we generate an execution file at this time? It is also easy to enter GCC hello. C-O hello./libtest. So. Enter./Hello. Then, you can verify that the execution file is running correctly. When writing a static library, I said that the static library is linked to the execution file, but the dynamic library does not. Friends can make a small experiment, delete libtest. So, and enter./hello. At this time, you can see if the system returns an error?
At this time, some friends will ask, how should the DLL be written in windows? In fact, it is not difficult, as long as you make a slight change in test. h. Other steps are similar to static library operations.
#ifndef _TEST_H#define _TEST_H#ifdef USR_DLL#define DLL_API _declspec(dllexport)#else#define DLL_API _declspec(dllimport)#endifDLL_API int add(int a, int b);#endif