Linker in the 1.C language
(1) each C language source file is compiled to generate the target file, these target files will eventually be linked together to generate the executable file.
(2) The main function of the linker is to handle the parts of each module that are referenced to each other, so that each module can be accurately connected.
2. Static link
The linker joins the contents of the library directly into the executable program when linked
① compile Static library source code:gcc –c lib.c –o lib.o
② generating a static library file:ar –q lib.a lib.o/ / package lib.o with other files in LIB.A
③ using static library compilation:gcc main.c lib.a –o main.out
3. Dynamic linking
The executable program loads the library dynamically at runtime to link, and the contents of the library are not entered into the executable program.
① compiled dynamic library Source:gcc –shared dlib.c –o dlib.so
② using Dynamic Library compilation:gcc main.c -ldl –o main.out
4.dlopen, Dlsym, dlclose use dynamic Library
In order to make the program easy to expand, with universal, can be used plug-in form. The asynchronous event-driven model is used to ensure that the main program logic is not changed, and the various services are loaded in the form of dynamic link libraries, which is called plug-ins. Linux provides a convenient system call to load and process a dynamic-link library.
① Open Dynamic Library:Dlopen
② Find a function in the dynamic library and return the calling address:dlsym
③ Close the dynamic library:dlclose
DLIB.C Library
char* name () { return "Dynamic Lib";} int add (int a, int b) { return a + b;}
TEST.c
#include <stdio.h> #include <dlfcn.h>int main () { //open (load) dynamic library void* pdlib = Dlopen ("./dlib.so", Rtld_lazy); Char* (*pname) (); Int (*padd) (int, int); if (pdlib! = NULL) { //Find function address pname = Dlsym (pdlib, "name"); Padd = Dlsym (Pdlib, "add"); if (pname! = null) && (padd! = null)) { printf ("Name:%s\n", pname ()); printf ("Result:%d\n", Padd (2, 3)); } Close the dynamic library dlclose (pdlib); } else { printf ("Cannot open lib ... \ n"); } return 0;}
gcc-shared Dlib.c-o dlib.so
GCC Test.c-o TEST.O-LDL
Resources:
Www.dt4sw.com
Http://www.cnblogs.com/5iedu/category/804081.html
-----------The process you use--------------------
Dynamic libraries:
Cat Makefileall: gcc -w -g-std=c99-fpic-shared-i. /include/zookeeper-i. /inlcude-l. /lib/-LZOOKEEPER_MT-o ecox_rws_helper.so zookeeperhelper.c clusterinfohelper.c gcc -o Test main.c Ecox_rws_helper.soclean: RM *. So test[[email protected] src]#
Static libraries:
Cat Makefile_staticall: gcc -g-std=c99-i. /include/zookeeper-o zookeeperhelper.o-c zookeeperhelper.c-i. /include-l. /lib-lzookeeper_mt gcc -g-std=c99-o clusterinfohelper.o-c clusterinfohelper.c-i. /include #这里无法将zookeeper_mt库合并到这个静态库中, because that's a dynamic library .... Dynamic library ar-Q Ecox_rws_ can only be specified when the code is connected HELPER.O zookeeperhelper.o clusterinfohelper.o gcc main.c ecox_rws_helper.o-o test_static-l. /lib-Lzookeeper_mtclean: rm *.O test_static
Original:
Cat makefile_orgall:ecox_rws_helperecox_rws_helper:zookeeperhelper.o main.o clusterinfohelper.o gcc -g-l. /lib-lzookeeper_mt-o [email protected] $^zookeeperhelper.o:zookeeperhelper.c gcc -g-std=c99- I.. /include/zookeeper-o [email protected]-C$^ clusterinfohelper.o:clusterinfohelper.cgcc -g-c- o [email protected] $^main.o:main.c gcc -g-c-o main.o main.c.phony:cleanclean: RM *.O
The use and practice of C language dynamic library and static library