The production and use of dynamic library under Linux

Source: Internet
Author: User

1, the production of dynamic link library

The dynamic link library has no main function compared to a normal program. Generate. So dynamic link library files with-shared and-fpic compilation parameters. When the program calls the library function

Just link to this library.

1) write dynamic library code

Define header file reader.h

#ifndef Reader_h_ #define Reader_h_int open (char *name); int Close (char *name); #endif // Reader_h_

Writing function Body reader.c

#include <stdio.h>int open (char *name) {    printf ("%s opened.\n  ", name);     return 0 ;} int Close (char *name) {    printf ("%s closed.\n")  , name);     return 0 ;}

2) Generate Dynamic Library files

gcc -shared-fpic reader.c-o libreader.so

2, the use of dynamic library

First step: Write the test code TESTLIB.C

" Reader.h " int Main () {    char readername["81Reader";    Open (readername);    Close (readername);        return 0 ;}

Step Two: Call the Dynamic library (-L indicates the path to the dynamic-link library)

gcc testlib.c-o testlib-l./-lreader

Step three: Test output

81Reader Opened.81reader closed.

The production and use of dynamic library under Linux

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.