Linux static libraries, shared libraries

Source: Internet
Author: User
Tags strtok

First, what is the library

In essence, a library is a binary form of executable code that can be loaded into memory by the operating system. Because of the different nature of Windows and Linux, the binary of the libraries is incompatible. The function libraries supported by Linux operating system are classified into static and dynamic libraries, and dynamic libraries are also called shared libraries. The Linux system has several important directories for storing the appropriate libraries, such as/lib/usr/lib.  Second, the static function library, the dynamic function library A. The name of such a library is generally libxxx.a; the file compiled from the static function library is larger, because all the data of the library is integrated into the target code, and his advantages are obvious, that is, the compiled execution program does not require external library support, because all the functions used have been compiled into the executable file. Of course this is also called its disadvantage, because if the static function library changes, then your program must be recompiled, and the volume is larger. B. This type of Kurdish name is generally libxxx.so, the dynamic library is also called the shared library; compared to the static function library, the dynamic function library is not compiled into the target code at compile time.The corresponding function in the function library is called when your program executes to the relevant function, so the executable file generated by the dynamic library is relatively small. Since the library is not integrated into your program, it is dynamically applied and invoked while the program is running.The corresponding library must be provided in the running environment of the program.。 The change of dynamic function library does not affect your program, so the upgrade of dynamic function library is more convenient. And if multiple applications are going to use the same library, a dynamic library is a great fit to reduce the volume of your application. Note: Both the static function library and the dynamic function library are generated by the *.O target file. Iii. creation of a library of functions A. Creation of a. Static function library AR-CR libname.a test1.o test2.o ar: The static function library creates a command-c:create meaning-r:replace means that the currently inserted module name already exists in the library, replacing the same name Module. If there is a module in several modules that does not exist in the library, AR displays an error message and does not replace other modules with the same name. By default, the new members are incremented at the end of the Kurdish position. B. Creation of a dynamic function library Gcc-shared-fpic-o libname.so test1.c test2.c

-fpic: Generating code location-independent code



-shared: Build shared library Four, static and dynamic library use cases:ADD.C#include int Add (int a,int b){return a + B;}SUB.C#include int sub (int a,int b){return a-B;} head.h #ifndef _head_h_#define _HEAD_H_extern int Add (int a,int b);extern int sub (int a,int b);#endifmain.c#include int main (int argc,char *argv[]){int A, b; if (argc < 3){fprintf (stderr, "Usage:%s argv[1] argv[2].\n", argv[0]);return-1;} a = atoi (argv[1]);B = atoi (argv[2]); printf ("a + b =%d\n", add (b));printf ("A-B =%d\n", Sub (A, a)); return 0;}Generate a static library



To generate a dynamic library:



To use the generated generated library:



Where-l specifies the location of the function library lookup, and note that L is followed by '. ', which means that the lookup of-L in the current directory specifies the function library name, where Lib and. A (. so) are omitted. Note:-L is the specified lookup location,-l specifies the name of the library that needs to be manipulated. From the running results above, we can see: A. When the dynamic and static libraries exist simultaneously, GCC uses a dynamic library by default. If you force the use of a static library, you need to add-static option support. B. The executable file generated by the dynamic library test1 not run properly. C. The executable program linking the static library is significantly larger than that of the linked dynamic library. Five, let the link dynamic Library executable program to run normally. When the system loads executable code, it can know the name of the library it depends on, but it also needs to know the absolute path. In this case, the system dynamic Loader (Linker/loader) is required. For the ELF format executable program, is done by ld-linux.so*, it has searched elf file Dt_rpath segment---Environment variables ld_library_path,/etc/ld.so.cache file list,/usr/lib,/ The Lib directory loads the library file into memory after it is found. A. One of the most straightforward methods is to copy the generated dynamic library to/usr/lib or/lib.

B. Using the LD_LIBRARY_PATH environment variable, this environment variable is not available by default in the Ubuntu operating system and needs to be added manually



C. Dynamic in the other directory installed, if you want the operating system can find it, you can pass the steps <1> new and edit the/etc/ld.so.conf.d/my.conf file, add the path of the directory where the library is located <2> Execute the ldconfig command to update the Ld.so.cache file

At this point, the executable that executes the linked dynamic library can run normally. Vi. View the symbols in the Library A.NM command to print out all the symbols involved in the library. A library can be either a static library or a dynamic one. The common three symbols:<1> are called in the library, but are not defined in the library (indicating that additional library support is required), use U to represent <2> functions defined in the library, and T for <3> "weak" symbols, although they are defined in the library, However, it may be overridden by a symbol with the same name in another library, denoted by W.



B.LDD command to view a shared library that an executable program relies on



The dynamic loading library is generated by gcc-shared called Dynamic Libraries (shared libraries), where dynamic libraries are run in two ways. Dynamic linking in this way, the executable program just makes a dynamic link, and when it needs to use the function in the dynamic library, there is the loader hermit's load. B. Dynamic loading in this way, inside the executable program, we can use a function like Dlopen () to manually load the Dlsym () function to find the entry address where we want to invoke the function, and then make the call. This way, is widely used in the writing plug-in program. Related APIs:

<1>dlopen () opens a new dynamic library and loads it into memory. This function is mainly used to record symbols in the library, which are not known at compile time. The Dlopen () function requires two parameters: a file name and a flag. A. The file name is the name of the dynamic library that we have touched before, and if it is an absolute path, such as:/home/cyg/worddir/libname.so, then dlopen directly to the specified path to open the dynamic library. If the path is not specified, only the name of a dynamic library is specified, and Dlopen searches for the Dt_rpath segment of the elf file, the environment variable Ld_library_path, the/etc/ld.so.cache file list, the/lib, the/usr/ The Lib directory loads the library file into memory after it is found.

B. The flag indicates whether the library's dependencies are calculated immediately.


Often a library is also dependent on other libraries, that is, when this function is implemented, other library functions are called. Functions that are not implemented in this library are called bits-defined symbols.
If the flag is set to Rtld_now, these undefined symbols are parsed before the Dlopen function returns. If set to Rtld_lazy, it will be parsed when needed. Return value: the Dlopen () function returns a handle as the first parameter of the Dlsym () function to obtain the address of the symbol in the library. With this address, you can get a pointer to a specific function in the library and call the corresponding function in the load library. <2>dlerror () When the dynamic link library operation function fails, Dlerror can return an error message, and a return value of NULL indicates that the operation function executed successfully. <3>void *dlsym (void *handle,char *symbol); Dlsym returns the execution code address of the function corresponding to the symbol, based on the dynamic link library operation handle (handle) and symbol. From this address, the corresponding function can be executed with parameters. such as program code: INT (*add) (int x,int y),//function pointer handle = Dlopen ("xxx.so", rtld_lazy);//open Shared library add = Dlsym (handle, "add");// Gets the address of the add function in the shared library value = Add (12,34);//Call the Add function <4>int dlclose (void *handle);d lclose The dynamic-link library used to close the specified handle. Only when the usage count for this dynamic link library is 0 o'clock is it really uninstalled by the system. Case:#include#include#include#include int test_dl (char *pso,char *pfu){void *handle;Int (*ptest) (int x,int y); if ((Handle = Dlopen (pso,rtld_lazy)) = = NULL){printf ("%s.\n", Dlerror ());return-1;} if ((Ptest = Dlsym (handle,pfu)) = = NULL){printf ("%s.\n", Dlerror ());return-1;} printf ("Ptest Complete:%d.\n", Ptest (12,13));Dlclose (handle); return 0;} int main (int argc,char *argv[]){Char buf[100];Char *pso,*pfun;printf ("Input xxx.so:function_name.\n");While (1){printf (">");fgets (buf,sizeof (BUF), stdin);Buf[strlen (BUF)-1] = 0; PSO = StrDup (Strtok (buf, ":"));Pfun = StrDup (Strtok (NULL, ":"));test_dl (pso,pfun);}return 0;}

Operation Result:

Linux static libraries, shared libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.