C Language Development in Linux (dynamic library)

Source: Internet
Author: User

[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

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.