Static Libraries and Dynamic (Shared) Libraries

Just to expand:

ELF file (Executable and Linkable Format): Executable and linkable format file.

ELF relocatable object file format:


Format of the ELF executable:


Static library: (.a)

All build systems provide a mechanism to package all related object files into a single file, a static library, which can be used as input to the linker. (Generally composed of .o files, that is, the packaging of .o files)

When the program is compiled and linked, the code of the library is linked into the executable file, and the static library is no longer needed when the program is running.


How to pack?

ar -rc lib library name.a (library file name) xxx.o ex: ar -rc libmymath.a add.o sub.o

Static library default search path:

1./lib /usr/lib

2.-L specifies the path to the library e x: gcc main.c -L . -lmymath

When the static library is called, which function is used and which function (.o file) is called .


Cons: Static libraries, like all software, require regular maintenance and updates. If we want to use the latest version of a library, we must somehow know that the library is up to date, and then explicitly add our

The program reconnects with the updated library.

Dynamic (shared) library: (.so)

(ELF file, executable program) A shared library is an object module that, at runtime, can be loaded into any memory address and linked with a program in memory.

The dynamic library only links the code of the dynamic library when it is running, and how many programs share the code that uses the library.

An executable file linked to a dynamic library only contains a table of the function entry addresses they use, not the entire machine code of the object file where the external function resides

Before the executable starts running, the machine code for the external function is copied by the operating system from the dynamic library file on disk into memory. This process becomes dynamic linking.

Dynamic libraries can be shared among multiple programs, so dynamic linking results in smaller executables, saving disk space.


How to generate dynamic library?

gcc -fPIC -shared -o lib库名.so xxx.c ex:gcc -fPIC -shared -o libmymath.so  add.c sub.c

Link to dynamic library:

gcc main.c -L . -lmymath

ldd view static library link status

ldd a.out

Use of dynamic library:

1.vim /etc/ld.so.conf.d/XX.conf -- add the path of the dynamic library to vim /etc/ld.so.conf.d/mymath.conf .

2. ldconfig link dynamic library

Open dynamic library with file: runtime loading of dynamic library: -ldl ex: gcc main.c -ldl

void *dlopen(const char *filename, int flag); //Dynamic library name, when to load the dynamic library into memory (RTLD_LAZY (when to use, when to load))

//The return value represents the handle of the dynamic library

void *dlsym(void *handle, //The return value of dlopen

const char *symbol); //The symbol name of the function (function name) returns the address of the function---the function pointer to receive

int dlclose(void *handle); //Close the file opened by dlopen

#include <stdio.h>
#include <dlfcn.h>

#include "add.h"
#include "sub.h"

typedef int FUNC(int ,int);

intmain()
{
    void *dp = dlopen("libmath.so", RTLD_LAZY);

    FUNC *paf, *psf;
    
    paf = (FUNC*)dlsym(dp, "add");
    psf = (FUNC*)dlsym(dp, "sub");
    
    printf("10 + 20 = %d\n", paf(10,20));
    printf("10 - 20 = %d\n", psf(10,20));
    dlclose(dp);

    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324640382&siteId=291194637