10. Library of functions-static and shared libraries

Source: Internet
Author: User
Tags naming convention
10. Library of functions-static and shared libraries

A function library is a collection of several target files in a format that is divided into two types: static and shared libraries . When an application links a static library, it embeds the required static library functions into the executable (not all static libraries), and when the shared library is linked it only holds the information needed to load the target object in the executable file, and the target object is loaded into memory when it is actually called.

1. The static library is created and updated by the AR tool, the library file naming convention is libxxx.a, starting with Lib,. A as the filename suffix. In ANSI C, for example, it defines a broad set of standard I/O, string operations such as Atoi, printf, strcpy, etc., exist in the LIBC.A library, floating-point functions exist in the LIBM.A library. Its disadvantage is that the program runtime requires a copy of the objects required in the static library, which is not easy to maintain and update

2. The shared library calls the required function library at run time , the library file naming convention is libxxx.so, and the. so as the filename suffix. For example, call the thread, socket and other libraries, only the corresponding add compiler option-lthread,-lsocket option (Note: Always put the-lxxx option at the far right of the compile command line arguments). Use the LDD command to view the shared libraries that are required by the current executable file at run time. It has the advantage that each program that links to the shared library shares its same copy (a. So file without being copied or embedded in the executable, and a copy of the library can be shared by several different running processes)

3.-fpic option compiles a shared library that generates location-independent code that can be loaded at any address or shared by multiple processes at run time

In addition, the application can dynamically load the shared library at any time of the run, not necessarily loading the shared library immediately at startup, but dynamically loading the shared library when needed. The shared library is now called a dynamic-link library. The Linux system provides a set of APIs for dynamic-link libraries that allow applications to load and link shared libraries at run time

#include <dlfcn.h>

void *dlopen (const char *file,int flag) #打开动态链接库, the flag parameter is generally selected Rtld_lazy, indicating deferred symbol resolution to code in the execution library

void *dlsym (void *handle,char *symbol) #打开链接库句柄, take the execution address of the symbol

int dlclose (void *handle) #关闭共享库

const char *dlerror () #检查动态链接库操作是否失败

With these interfaces, we can use our own dynamic link libraries, and then invoke the functions and objects in the shared library. The invocation step is simple: Open the dynamic-link library file, get the address of the calling function, invoke it, and finally close the dynamic-link library. Note Compile-time command-line arguments-rdynamic and-LDL

Gcc-rdynamic xxx.c-o p-ldl #-rdynamic for resolving global symbols in shared libraries,-LDL for linked DL libraries

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.