Linux compiles dynamic library and static library compilation process, dynamic loading of dynamic library

1 Introduction

Why use library files? You need to use the printf function to write code, including stdio.h. This is to call the functions in the C library. The more common things can be made into libraries for everyone to use. Also, during project development, especially cross-company cooperative development, the source code cannot be given casually. I will make my code module into a library file for you, and your code will load my library file to generate an application that is convenient for debugging your code. program.

2. Static compilation

Static compilation is to merge the library files into the application program. The advantage is that it can be run directly on another device. Unlike dynamic libraries, the application program and library files must be moved to the new device at the same time before they can be used.

2.1 Compile the static library

The following simple code is saved in mylib.c, which provides a printing function:

#include <stdio.h>
#include "mylib.h"

void LibTest()
{
    printf("Exe LibTest In mylib.c\n");
}

The corresponding mylib.h file is as follows:

#ifndef __MY_LIB_H__
#define __MY_LIB_H__

extern void LibTest(void);

#endif

Compile the .c file into a .o file and execute the command:

gcc -c mylib.c -o mylib.o

Compile and generate a static library file, execute the command:

ar -r libmylib.a mylib.o

Where -r means to create a new library file mylib.a and add mylib.o to it

2.2 Using a static library

Call the code in the static library file in the main.c file:

#include <stdio.h>
#include "mylib.h"

int main()
{
    LibTest();
    
    return 0;
}

Compile the executable program:

gcc -o app main.c -L ./ -lmylib -static

Among them, -L specifies the path of the static library -l specifies the name of the static library, here libmylib does not need to add the previous lib, -static specifies static compilation.

The compiled app program can be directly launched and run manually, and the running results are as follows:

3. Dynamic compilation

The dynamic library is loaded in real time when the program is running, and does not need to be compiled into the application program. The advantage of this is to reduce the size of the application program. The disadvantage is that the corresponding mobile dynamic library needs to be moved when the program is moved.

3.1 Compile dynamic library

Also use the above code, execute the following command to generate a dynamic library file:

gcc mylib.c -fPIC -shared -o libmylib.so

Among them, -fPIC means to generate position-independent code, and -shared means to generate dynamic library files

3.2 Using dynamic libraries

Copy the dynamic library to /usr/lib:

cp libmylib.so /usr/lib

Compile the executable program:

gcc -o appDT main.c -L. -lmylib

The compiled program can be run directly, considering the permission to access the library file, add sudo to run:

 3.3 Volume Comparison

Comparing the executable program size of the compiled static library and the executable program of the dynamic library, the previous statement can be verified:

 3.4 Dynamic loading and use of dynamic libraries

The dynamic loading calling code is as follows:

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

typedef void (*pJustPtf)(void);

int main()
{
    void * handle = NULL;
    pJustPtf  ptf = NULL;

    // 打开当前目录下的so库文件
    handle = dlopen ("./libmylib.so", RTLD_LAZY);

    if(handle != NULL)
    {
        // 从库文件中查找LibTest函数
        ptf = dlsym(handle, "LibTest");

        // 调用LibTest函数
        ptf();
    }
    
    return 0;
}

Compile the executable program with the following command:

gcc main.c -o app123 -ldl

Just run it directly:

 

Guess you like

Origin blog.csdn.net/cesheng3410/article/details/129439035