Easily learn about Linux dynamic libraries

Source: Internet
Author: User

At present, Linux systems are increasingly popular with computer users. As a result, many people begin to learn about Linux. During this period, you may encounter the knowledge of writing Linux dynamic libraries, this section describes how to write Linux dynamic libraries.

4. Writing dynamic libraries

1. Overview

A dynamic library generally ends with. so, which means shared object.

The basic generation steps are as follows:

(1) Compile Function Code

(2) compile and generate a dynamic library file with the-shared and-fpic options. The library file name starts with lib and ends with. so.

There are two usage methods: implicit call and display call.

Implicit calling is similar to static library usage, but you need to modify the configuration file/etc/ld of the dynamic link library. so. conf; display call is to use dlopen, dlsym, dlerror, dlclose and other system functions in the main program. The specific call method is described in "5. Dynamic library call.

2. Write the simplest dynamic library file

For easy comparison, we still use files in the static library as an example.

Write the following two files and place them in the same directory.

 
 
  1. Myalib. h // static library header file
  2. Myalib. c // static library implementation file
  3. // Myalib. h file content
  4. Void test ();
  5. // Myalib. c file content
  6. # Inlcude<Stdio. h> 
  7. Void test ()
  8. {
  9. Printf ("test \ n ");
  10. }

3. compile and generate a dynamic library. The library file name starts with lib and ends with. so.

Gcc-fpic-shared-o libtest. so myalib. c

In this case, a libtest. so file is generated. The above is the knowledge of writing Linux dynamic library.

  1. Configuration notes: configure the DNS server in Linux
  2. How to display Chinese Characters in RedHat Linux 5
  3. How to disable SELinux in Redhat Enterprise Linux
  4. Install the KDE package in Linux
  5. Redhat Linux Remote Desktop configuration

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.