The concept of a library file
In many cases, source code files can also be shared by multiple programs. So the first step in reducing your workload is to compile the source code files only once, and then link them to a different executable file when you need them. Although this technique can save compile time, its disadvantage is that you still need to name all the target files when linking. In addition, a large number of target files will be scattered in various directories on the system, resulting in confusion in the contents of the directory.
To solve this problem, you can organize a set of target files into a unit called an object library. The object library is divided into two types: static and shared. Shared libraries are a more modern object library, which is more advantageous than static libraries.
A static library is also called an archive, which is a library provided by the Unix/linux system.
(1) Characteristics of the static library
1 static library in use, directly copy the code to the target file.
2 Advantages: Do not need to jump, the efficiency is better, from the static library file.
3 Disadvantage: The target file will be relatively large, modification and maintenance are not very convenient.
(2) features of shared libraries
1 The shared library in use, directly to the point of the corresponding address copied over.
2 Advantages: The target file is relatively small, modification and maintenance are convenient.
3 disadvantage: Need to jump, low efficiency, can not be separated from the shared library files.
(3) Basic command
Ldda.out => represents viewing shared library information that a.out is linked to
Gcc/cc-static xxx.c => Representations are handled in a static library
The comparison found that the static library method generated a larger file.
steps for building and using static libraries
(1) Generating steps for static libraries
1) Write source code (xxx.c file)
VI add.c File
2 only compile and not chain delivery into the target file (XXX.O end of the reduction)
Gcc/cc-c ADD.C
3) generating Static library files
ar-r/* Insert */lib library name. A target file
Ar-r libadd.a ADD.O
Attention:
Name rule for static library file name: Beginning with Lib, ending with. A
The static library file name and library name are different concepts, and the library name has no prefix and suffix
(2) The use of static library steps
1 Write test source code (XXX.C)
VI main.c File
2) compile-only not chain delivery into the target file (XXX.O)
Gcc-c MAIN.C
3 Link Test files and static library files, links in the way there are three kinds:
A. Direct links
GCC MAIN.O libadd.a
B. Using compilation options to link
GCC main.o-l Library name-L library file path
GCC main.o-l add-l.
C. Configuring environment Variables Library_path
Export library_path= $LIBRARY _path:.
GCC main.o-l Add
steps for building and using shared libraries
(1) Build steps for shared libraries
1) Write source code (XXX.C)
VI add.c File
2) compile-only not chain delivery into the target file (XXX.O)
Gcc-c-fpic/* Small mode, less code * * ADD.C
3 Generating Shared library files
gcc-shared destination file (XXX.O)-O Lib library name. So
gcc-shared/* Share * * Add.o-o libadd.so
(2) Use steps for shared libraries
1 Write test source code (XXX.C)
VI main.c File
2) compile-only not chain delivery into the target file (XXX.O)
Gcc/cc-c MAIN.C
3 Link Test files and shared library files, links in the way there are three kinds:
A. Direct links
GCC MAIN.O libadd.so
B. Using compilation options to link
GCC main.o-l Library name-L library file path
GCC main.o-l add-l.
C. Configuring environment Variables Library_path
Export ld_library_path= $LD _library_path:.
GCC main.o-l Add
Attention:
The use of a shared library requires that you configure the value of the environment variable Ld_library_path, primarily to solve the problem of not finding a shared library at run time.
dynamic loading of shared libraries
(1) Dlopen function
#include <dlfcn.h>
void *dlopen (const char *filename, int flag);
First parameter: The name of a shared library function in the form of a character
Second parameter: Flag
rtld_lazy-Delay Loading
Rtld_now-Load Now
return value: Generic class pointer, successfully return handle, temporarily understand the head address, failure returns null
function function:
Primarily used to open and load dynamic libraries
(2) Dlerror function
Char *dlerror (void);
function function:
Used primarily to get details of the most recent error that occurs when a function call procedure such as Dlopen
(3) Dlsym function
void* dlsym (void *handle, const char *symbol);
First argument: Handle, which is the return value of the Dlopen function
Second parameter: a symbol in the form of a string that represents a function name
Return value: Successfully returns the address of the function in memory, failure returns null
function function:
Mainly used to get the corresponding address in memory according to the handle and function name
(4) Dlclose function
int dlclose (void *handle);
function function:
Used primarily to turn off the shared library specified by the parameter handle, successfully return 0, fail to return 0, and reclaim the memory space occupied by the shared library when the shared library is no longer being used by any program.
Attention:
Additional options are required when compiling links:-ldl
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main (void)
{
/ /Open a dynamic library
void *handle = Dlopen ("./dynamic_add/libadd.so", rtld_lazy);
Interpretation on success, return value NULL indicates open failure
if (NULL = = handle)
{
//print Failure reason
printf ("%s\n", Dlerror ());
Exit ( -1);
}
printf ("Open shared library succeeded.) \ n ");
Read the function address in the dynamic library (the function pointer must be the same type as the function that needs to be read)
int (*p_func) (int, int) = Dlsym (Handle, "add");
Determines whether the read was successful, and the return value NULL indicates a read failure
if (NULL = = P_func)
{
printf ("%s\n", Dlerror ());
Exit ( -1);
}
Call the dynamic library function
printf ("%d\n", P_func (2, 8));
Finally, the dynamic library is closed, and a return value of 0 indicates shutdown failure
if (0!= dlclose (handle))
{
printf ("%s\n", Dlerror ());
Exit ( -1);
}
printf ("Close shared library succeeded.) \ n ");
return 0;
}
Summary:
because shared inventory has many advantages over static libraries, shared libraries are much more common on contemporary unix/linux systems. The main source of shared libraries is the fact that when a program is linked to a library, a copy of the target module required by the program is not included in the resulting executable file. Instead, the (static) linker will add information about the shared library that the program needs at run time in the executable file. When the file is executed, the dynamic linker uses this information to load the shared library that is required. At run time, all programs that use the same shared library share a copy of the library in memory. Because shared libraries are not replicated to executables and all programs use a single copy of the shared library in memory at run time, shared libraries can reduce the disk space and memory required by the system.