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.
- Myalib. h // static library header file
- Myalib. c // static library implementation file
- // Myalib. h file content
- Void test ();
- // Myalib. c file content
- # Inlcude<Stdio. h>
- Void test ()
- {
- Printf ("test \ n ");
- }
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.
- Configuration notes: configure the DNS server in Linux
- How to display Chinese Characters in RedHat Linux 5
- How to disable SELinux in Redhat Enterprise Linux
- Install the KDE package in Linux
- Redhat Linux Remote Desktop configuration