Yuchuan_Linux_C programming of the four dynamic library (shared library) production

First, the overall outline

 

Second, the production shared libraries

1. Naming:

    lib + name + .so

2. Production steps:

    1) independent of the position code generate (generate position-independent .o)
    2) .o packaged into the shared library (DLL)

3. Publish and use of shared libraries:

4. solve the problem of dynamic libraries can not be loaded when the program is executed: 

    1) into the library catalog system - are not allowed

    2) temporary test

          Environment variables: LD_LIBRARY_PATH = DLL path is provided to the variable
          value is set into the system environment variable: export LD_LIBRARY_PATH
          when the terminal is closed, setting fail

    3) is not commonly used method (permanent setting): 

          .Bashrc file in the home directory to add one sentence: export LD_LIBRARY_PATH = absolute path of a dynamic library catalog
           .bashrc changes are complete, need to restart the terminal

    4) modify the configuration file  

          . a need to find dynamic linker configuration files - /etc/ld.so.conf
          . b path dynamic library written to the configuration file - absolute path
          c update -. sudo ldconfig -v

5. Advantages and disadvantages:

Third, the production example of dynamic library  

1. Production steps

(1) regardless of the position of compiled code number (FIG shared library), generates .o object files, key parameters -fPIC

            Process virtual address space

(2) the .o file is packaged, the key parameters -shared

(3) will be released together with the library header files

  • Generates .o object files

And the contents of the file path as above, into the src directory and generates .o object files:

[root@centos1 src]# gcc -fPIC -c *.c -I ../include/
[root@centos1 src]# ll
总用量 16
-rw-r--r--. 1 root root   63 4月  20 14:53 add.c
-rw-r--r--. 1 root root 1240 4月  20 15:20 add.o
-rw-r--r--. 1 root root   63 4月  20 14:24 sub.c
-rw-r--r--. 1 root root 1240 4月  20 15:20 sub.o
  • 将.o文件打包生成动态库
[root@centos1 src]# gcc -shared -o libCalc.so *.o
[root@centos1 src]# ll
总用量 24
-rw-r--r--. 1 root root   63 4月  20 14:53 add.c
-rw-r--r--. 1 root root 1240 4月  20 15:20 add.o
-rwxr-xr-x. 1 root root 5817 4月  20 15:21 libCalc.so
-rw-r--r--. 1 root root   63 4月  20 14:24 sub.c
-rw-r--r--. 1 root root 1240 4月  20 15:20 sub.o

2. 使用

  • 使用生成的动态库,-L 指定动态库路径 -l 指定库名(gcc main.c -o main -I include/ -L ./lib -lCalc) 

退出到calc目录下,将动态库拷贝到lib目录下并编译mian.c并执行main:

[root@centos1 calc]# gcc main.c -o main -I include/ -L ./lib -lCalc
[root@centos1 calc]# ll
总用量 24
drwxr-xr-x. 2 root root 4096 4月  20 15:07 include
drwxr-xr-x. 2 root root 4096 4月  20 15:29 lib
-rwxr-xr-x. 1 root root 7066 4月  20 15:30 main
-rw-r--r--. 1 root root  179 4月  20 14:57 main.c
drwxr-xr-x. 2 root root 4096 4月  20 15:29 src
[root@centos1 calc]# ./main
./main: error while loading shared libraries: libCalc.so: cannot open shared object file: No such file or directory

无法找到动态库目标文件,解决方法有三个:

   (1)拷贝到/lib下(不允许,容易与别人库命名重合)

   (2)将库路径增加到环境变量LD_LIBRARY_PATH中(不是特别推荐)

[root@centos1 calc]# export LD_LIBRARY_PATH=/home/xuejiale/src/calc/lib/:$LD_LIBRARY_PATH
[root@centos1 calc]# ./main
10 + 10 is 20
10 - 10 is 0

(3)配置/etc/ld.so.conf文件,增加 /home/xuejiale/src/calc/lib/ 路径,执行 ldconfig -v,并执行main

/home/xuejiale/src/calc/lib/
[root@centos1 calc]# ./main
10 + 10 is 20
10 - 10 is 0

3. 优缺点总结

   缺点:

  • 执行时需要加载动态库,相对而言,比静态库慢
  • 发布应用时需要同步发布动态库

   优点:

  • 执行程序体积小
  • 库变更时,一般不需要重新编译应用

动静态库总结:

     静态库特点:

  • 库的代码会编译进程序里面,所以静态库编译的程序比较大。
  • 由静态库编译的程序不用依赖于系统的环境变量,所以环境变量有没有这个库文件,也可以运行。

    动态库特点:

  • 库的代码不会编译进程序里面,所以动态库编译的程序比较小。
  • 由动态库编译的程序依赖于系统的环境变量有没有这个库文件,没有则运行不了。

 

Guess you like

Origin www.cnblogs.com/YuchuanHuaying/p/11129242.html