Linux lib description

Source: Internet
Author: User
Linux lib description-general Linux technology-Linux programming and kernel information. The following is a detailed description. I. lib type:
And static library in windows (. lib) and dynamic library (. dll), linux also has a static library (the static library file suffix is. a) and shared library (shared library file suffix. so). the/usr/lib directory contains both the static and dynamic versions of a library.

"An archive (or static library) is simply a collection of object files stored as a single file.
When you provide an archive to the linker, the linker searches the archive for the object files
It needs, extracts them, and links them into your program much as if you had provided those
Object files directly ."

"A shared library is similar to a archive in that it is a grouping of object files. However,
There are using important differences. The most fundamental difference is that when a shared library is
Linked into a program, the final executable does not actually contain the code that is
Present in the shared library. Instead, the executable merely contains a reference to
Shared library ."
"The object files that compose the shared library are combined into
Single object file so that a program that links against a shared library always provided des all
Of the code in the library, rather than just those portions that are needed ."
These are derived from Advanced Linux Programming.

It can be seen that both the static library and the shared library are a collection of obj files, but after static links, there is a copy of the obj required by the execution program, and after dynamic links, the execution program only contains a reference to the shared library. A Shared Library is equivalent to an obj file composed of multiple obj files. After the file is linked, all its code is loaded, no matter what you need or do not need it.
It seems that we can draw a conclusion:
The static link program has a larger storage space than the dynamic link, because the execution program contains the code copy in the library;
The dynamic link program has a larger runtime space than the static link, because it loads unnecessary code into the runtime space.

Ii. lib Compilation:
Static library compilation is actually a process of packaging. o files.
Ar-rc libfunc. a func1.o func2.o # compile f1.o and f2.o into static library libfunc.
For dynamic library compilation, use the gcc-fPIC-shared compilation option.
Gcc-fPIC-shared-o libfunc. so func1.o func2.o # compile f1.o and f2.o into dynamic library libfunc. so

Iii. lib call:
Static library calls are relatively simple, just like standard library function calls
Example: main. c calls the libfunc. a library under the./lib directory. the header file of this library is in the./inc directory and compiled:
Gcc-o test main. c-I./inc-L./lib-lfunc

When calling a shared library, pay attention to the location where the library file is stored. If the library file is not in the/lib or/usr/lib directory, set the LD_LIBRARY_PATH variable.
Example: main. c calls the libfunc. so library in the./lib directory. the header file of this library is in the./inc directory:
Gcc-o test main. c-I./inc-L./lib-lfunc
./Test # Run error: error while loading shared libraries: libfunc. so
This is because the program only stores the name of the shared library rather than the absolute path during the dynamic link, so we need to set the location of the library before running:
Export LD_LIBRARY_PATH =./lib
./Test # run successfully

Dynamic Loading of shared libraries: when there is only a shared library but no header file, or you want to dynamically load or uninstall the library at runtime, the linux dl library function: dlopen, dlclose, and dlsym help you with this. It is equivalent to a function prototype of LoadLibrary, FreeLibrary, and GetProcAddress in windows:
Void * dlopen (const char * filename, int flag );
Void * dlsym (void * handle, char * symbol );
Int dlclose (void * handle );
Example: The libfunc. so library under the./lib directory is dynamically loaded in main. c. The library has a function void print_str (const char *);
/* Load the database */
Void * handle = dlopen ("libfunc. so", RTLD_LAZY );
/* Obtain the function entry */
Void (* pt_str) (const char *);
Pt_str = dlsym (handle, "print_str ");
/* Call a function */
Pt_str ("hello world .");
/* Uninstall the database */
Dlclose (handle );

Iv. Description:
1. The shared library is especially suitable for sharing code among multiple programs, upgrading some functional modules of the program to implement the "plug-in" function of the program, while the static library is always used once and for all, after compilation, you do not need to run a pile of library files, and can run normally wherever it is placed.
2. When both the static and shared versions of the library exist in the searched library file directory, the linker preferentially uses the shared version. so, you can use the-static link option to specify the Link static version. a.
3. The dynamic library can export two special functions: _ init and _ fini. The former is called after the dynamic library is loaded, and the latter is called before the dynamic library is detached, we can use these two functions to do some special work. Note that you must use the-nostartfiles option when defining these two functions for compilation. Otherwise, the compiler reports a duplicate definition error.
4. The ldd command is used to view the shared library that the program depends on. It is also convenient for us to determine whether the shared library is found. The nm command is used to view the obj file (. so is also an obj) identifier (function, variable ).

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.