Generation and use of Linux static and dynamic libraries

1. Subfile Programming
Submodule programming idea: Suppose a project needs to use network, ultrasound, motor, the program is not all mixed up in the same file, but write the functions that network, ultrasound, motor need to call in different files, call directly in the main file.
Benefits:
a. Functional responsibility division
b. Easy debugging
c. Compact main program

2. Library
How do we create our own program function libraries on Linux using GNU's tools? A program function library simply means that a file contains some compiled code and data that can then be used by other programs. Program function libraries make the entire function more modular, easier to recompile, and easier to upgrade.Program function libraries can be divided into three types: static function libraries, shared function libraries, and dynamically loaded function libraries.

(a) Static Libraries
Static function library: is added to the target program before the program executes (compiles).
Advantage:
1. Static libraries load fast in packaged applications.
2. Publishers do not need to provide static libraries because they are already in the app for easy porting.
Disadvantages:
1. Complete copies of links to executable files, redundant copies if used many times
2. Troubleshooting updating, deploying and publishing

(b) Dynamic libraries (Dynamic libraries and shared function libraries are one thing, called shared object libraries on linux, file suffixes are.So, dynamic load function libraries on widows, file suffixes are.dll)
Dynamic function library: is called dynamically (temporarily) by the target program when the program is executed
Rules for naming shared libraries in Linux
libname.so.x.y.z
Fixed Represents Shared Library Shared Library Name Fixed Suffix Major Version Number Minor Version Number Release Version Number
Advantage:
1. Do not copy when linking. When the program runs, it is dynamically loaded into memory by the system for program invocation. The system is only loaded once. Multiple programs can be shared to save memory.
2. Program upgrade is simple because there is no source code of dynamic library in app. After upgrade, only the name of the library is unchanged, but the implementation is optimized, it can load successfully.
Disadvantages:
1. Loading speed is slower than static library
2. Publishers need to provide dependent dynamic libraries

3. Production of Library
The production format of static libraries xxx.a
1.gcc xxx.c-c //Generate xxx.o file
2.ar RCS libxxx.a xxx.o//xxx.o file Generate xxx.a static library file
The production format of dynamic libraries xxx.so
1,gcc -shared -fpic xxx.c -o libxxx.so
-shared Specifies Generate Dynamic Library
-fpic is a FPIC standard, the FPIC option operates during the compilation phase and must be used when generating the target file to generate location-independent code
4. Use of Libraries
Static Library
gcc xxx.c -lxxx -L ./ -o xxx
-l is the static library specified to be used, beginning and ending with the library name
-L tells the gcc compiler to look for static libraries from the path specified by-L. By default, look for static libraries from/usr/lib or/usr/local/lib
Dynamic Library
gcc xxx.c -lxxx -L ./ -o xxx
-l is the dynamic library specified to be used, beginning and ending with the library name
-L tells the gcc compiler to look for dynamic libraries from the path specified by-L. By default, look for dynamic libraries from/usr/lib or/usr/local/lib

Next we will demonstrate the generation and use of static and dynamic libraries
Prepare three files: calculator.c,calcufunc.c,calcufunc.h. The main function in calculator.c runs and calls the function in subfile calcufunc.c.
File calculator.c

#include <stdio.h>
#include "calcufunc.h"
int main()
{
        float a;
        float b;
        printf("please input value of a:\n");
        scanf("%f",&a);   
        printf("please input value of b:\n");
        scanf("%f",&b);
        printf("result is:\n");
        printf("%.2f+%.2f=%.2f\n",a,b,add(a,b));
        printf("%.2f-%.2f=%.2f\n",a,b,minus(a,b));
        printf("%.2f*%.2f=%.2f\n",a,b,mult(a,b));
        printf("%.2f/%.2f=%.2f\n",a,b,div(a,b));
        return 0;
}	

File calcufunc.c

#include <stdio.h>
float add(float a,float b)
{
        return a+b;
}
float minus(float a,float b)
{
        return a-b; 
}
float mult(float a,float b)
{
        return a*b; 
}
float div(float a, float b) 
{     
        if(b==0)
        {
                return -1;
        }
        return a/b; 
}   

File calcufunc.h

float add(float a,float b);
float minus(float a,float b);
float mult(float a,float b);
float div(float a,float b);

Static Compilation

//Generate calcufunc.o file
Ubuntu@Embed_Learn:~/learn/sepfile$ gcc calcufunc.c -c
Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.c  calcufunc.h  calcufunc.o  calculator.c
//Generate libcalcufunc.a file
Ubuntu@Embed_Learn:~/learn/sepfile$ ar rcs libcalcufunc.a calcufunc.o
Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.c  calcufunc.h  calcufunc.o  calculator.c  libcalcufunc.a
//Remove the calcufunc.c file to run MainPro without it
Ubuntu@Embed_Learn:~/learn/sepfile$ rm calcufunc.c
//Compile: Use the libcalcufunc.a static library, find the static library in the current path, and generate the MainPro executable
Ubuntu@Embed_Learn:~/learn/sepfile$ gcc calculator.c -lcalcufunc -L . -o MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.h  calcufunc.o  calculator.c  libcalcufunc.a  MainPro
//Execute MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ./MainPro
please input value of a:
9
please input value of b:
2
result is:
9.00+2.00=11.00
9.00-2.00=7.00
9.00*2.00=18.00
9.00/2.00=4.50

Dynamic Compilation

Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.c  calcufunc.h  calculator.c
//Generate dynamic library libcalcufunc.so
Ubuntu@Embed_Learn:~/learn/sepfile$ gcc -shared -fpic calcufunc.c -o libcalcufunc.so
Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.c  calcufunc.h  calculator.c  libcalcufunc.so
//Compile: Use libcalcufunc.so dynamic library, current path to find dynamic library, generate MainPro executable
Ubuntu@Embed_Learn:~/learn/sepfile$ gcc calculator.c -lcalcufunc -L . -o MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ls
calcufunc.c  calcufunc.h  calculator.c  libcalcufunc.so  MainPro
//Execute MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ./MainPro
./MainPro: error while loading shared libraries: libcalcufunc.so: cannot open shared object file: No such file or directory

The above error occurred during MainPro execution. Because during execution, dynamic libraries were found by default from/usr/lib or/usr/local/lib paths. So we want to set the location of the specified dynamic libraries, so how do we set it?
Method 1: Specify a dynamic library search path in the configuration file/etc/ld.so.conf. (There is a risk that this method will use administrator privileges.)

//Enter/etc Path
Ubuntu@Embed_Learn:~/learn/sepfile$ cd /etc
//Edit ld.so.conf file
Ubuntu@Embed_Learn:/etc$ vi ld.so.conf
//File ld.so.conf
//Includes all.Conf files under /etc/ld.so.conf.d/path
include /etc/ld.so.conf.d/*.conf
[1]+  Stopped                 vi ld.so.conf
//Enter/etc/ld.so.conf.d path
Ubuntu@Embed_Learn:/etc$ cd ld.so.conf.d
Ubuntu@Embed_Learn:/etc/ld.so.conf.d$ ls
i686-linux-gnu.conf  vmware-tools-libraries.conf  x86_64-linux-gnu_GL.conf
libc.conf            x86_64-linux-gnu.conf        zz_i386-biarch-compat.conf
//Edit libc.conff file
Ubuntu@Embed_Learn:/etc/ld.so.conf.d$ vi libc.conf
//File libc.conf
# libc default configuration
/usr/local/lib
[2]+  Stopped                 vi libc.conf
//Create mylibc.conf file
Ubuntu@Embed_Learn:/etc/ld.so.conf.d$ sudo touch mylibc.conf
//Edit mylibc.conf file
Ubuntu@Embed_Learn:/etc/ld.so.conf.d$ sudo vi mylibc.conf
//File mylibc.conf
/home/Ubuntu/learn/sepfile
[3]+  Stopped                 sudo vi mylibc.conf
//Back to/home/Ubuntu/learn/sepfile path
Ubuntu@Embed_Learn:/etc/ld.so.conf.d$ cd /home/Ubuntu/learn/sepfile
//Execute MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ./MainPro
please input value of a:
9
please input value of b:
2
result is:
9.00+2.00=11.00
9.00-2.00=7.00
9.00*2.00=18.00
9.00/2.00=4.50

Method 2: Specify the dynamic library search path through the environment variable LD_LIBRARY_PATH.

export LD_LIBRARY_PATH="path" 
//Query Current Path
Ubuntu@Embed_Learn:~/learn/sepfile$ pwd
/home/Ubuntu/learn/sepfile
//Add the current path to the environment variable LD_LIBRARY_PATH
Ubuntu@Embed_Learn:~/learn/sepfile$ export LD_LIBRARY_PATH="/home/Ubuntu/learn/sepfile"
//Execute MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ./MainPro
please input value of a:
9
please input value of b:
2
result is:
9.00+2.00=11.00
9.00-2.00=7.00
9.00*2.00=18.00
9.00/2.00=4.50

To make it easier to execute MainPro, we can use script files.

//Create lib.sh script file
Ubuntu@Embed_Learn:~/learn/sepfile$ touch lib.sh
//Edit lib.sh script file
Ubuntu@Embed_Learn:~/learn/sepfile$ vi lib.sh
//Add executable permissions to lib.sh script file
Ubuntu@Embed_Learn:~/learn/sepfile$ chmod +x lib.sh
//File lib.sh
export LD_LIBRARY_PATH="/home/Ubuntu/learn/sepfile"
./MainPro
//Execute lib.sh script file
Ubuntu@Embed_Learn:~/learn/sepfile$ ./lib.sh
please input value of a:
9
please input value of b:
2
result is:
9.00+2.00=11.00
9.00-2.00=7.00
9.00*2.00=18.00
9.00/2.00=4.50

Method 3: Specify the dynamic library search path of the program when compiling the target code.
You can specify the program's dynamic library search path when compiling the target code. This is specified by the gcc parameters "-Wl,-rpath=path".

Ubuntu@Embed_Learn:~/learn/sepfile$ gcc calculator.c -lcalcufunc -L . -Wl,-rpath=/home/Ubuntu/learn/sepfile -o MainPro
Ubuntu@Embed_Learn:~/learn/sepfile$ ./MainPro
please input value of a:
7
please input value of b:
2
result is:
7.00+2.00=9.00
7.00-2.00=5.00
7.00*2.00=14.00
7.00/2.00=3.50

Tags: Linux

Posted on Sun, 26 Sep 2021 14:10:15 -0400 by madspof