linux under the production and use of static and dynamic libraries

1. First, tell us about what is a static library, what is the dynamic library?

Static library:

  The names of these libraries are generally libxxx.a ; use of static libraries compiled into the files are large because all the data for the entire library will be integrated into the object code in, his advantage becomes apparent that the implementation of the compiled program the need for external library support, because the use of all functions have been compiled into executable files. Of course, this will be his disadvantage, because if the static library changes, then your program must be recompiled, and the volume is large.

Dynamic libraries:

  The names of these libraries are generally libxxx.so, dynamic libraries known as shared libraries; relative to a static library, dynamic library at compile time and has not been compiled into object code, when you call the program execution to the correlation function the function corresponding to the function library, and therefore the dynamic library executable file generated is relatively small. Since the library is not integrated into your application, but dynamic application and call the program is running, so the operating environment of the program must provide the appropriate library. Change the dynamic library does not affect your programs, so upgrading dynamic libraries more convenient. And if a plurality of applications have to use the same library, it is very suitable for a dynamic library, can reduce the size of the application.

2. Production Library

Static library of production:

① Preparation * .c file corresponding operations, and then to convert the file assembler * .c file * .o file

② perform ar -cr libxxxx.a * .o files     (explained: -c ----> represents create, create; -r -----> represents replace, replace; xxxx -----> represents the library first name)

③ When compiling main.c file: . -Lxxxx GCC -o main main.c -L    (-L .-------> represents a routing link libraries, the latter case -L remember. " "indicates the current directory;)

④ At this time, the static link library program just fine, this time by deleting xxxx library, implementation of the program will not be affected.

 

Establish a dynamic library

准备相应操作的*.c文件,然后将*.c文件转换至汇编文件*.o文件

②创建动态库:gcc  -shared  -fpic  -o  libxxxx.so  *.o  (-shared------>表示动态库(共享库);-fpic--->产生位置无关代码;xxxx------>表示动态库的名字)

③编译main.c文件:gcc -o dynamic dynamic.c -L. -lxxxx

④执行./dynamic   -------------------------------------会出错!!!!!!!!!!!!(找不到运行时依赖的动态库)

 

此时需要注意的是,我们现在还不能直接执行此程序,!!!!!!!!

我们需要在执行一步:export LD_LIBRARY_PATH="."   --------------------将当前目录设置环境变量,负责程序将找不到运行时所需要的动态库

完成此步,就可以执行了。

⑤再执行:./dynamic.   ---------------------------------OK

Guess you like

Origin www.cnblogs.com/single-dont/p/11391328.html