Preface
A static library is a collection of obj files.(In the target file, only the variables and functions in the file are parsed, And the referenced functions and variables are not parsed. This requires reference, parses unparsed functions and variables. The common reference target is). Generally, the static library uses ". a "is a suffix and the name format is generallyLibXxx.. Static library generated by the program ar.
The instance program is as follows:
Main. c
Print_hello.c
Generate a static link library, or add an obj file to an existing static library. The command format is:
The file obj_1 obj_2 In the ar-RC library...
Use the above instance program print_hello.c to create a static Link Library:
Use static Link LibraryMethod 1:
Method 2:
Note: In method 2, "-L./" is indispensable; otherwise, the following error occurs:
This is because the above command looks for the hello function library in the default path of the system, and we did not set libhello. database a is placed in the default search path of the system. Therefore, the path of the specified library function must be displayed as the current directory.
Note that,
The following two figures show the default search path of the header file and library file:
Preface
The dynamic link library is the library loaded when the program is running,After the dynamic link library is correctly installed, all programs can use the dynamic library to run the program.
The Dynamic Link Library name includes the alias (soname), real name (realname), and link name (linker name ):
Alias:LibXxx. So, the Library name in this form is the name to be searched by the compiler when executing the compilation command.
Real name: the real name of the dynamic link library. Generally, it is composed of a minor version number and a published version number based on the alias.
Link name: name of the library used for program connection.
:
Open/etc/selinux/config and change selinux = enforcing or permissive to disabled. Then save the disk and exit and restart the system.
Cannot open shared object file: No such file or directoryBecause the dynamic link library is not found during the program running. When a program is compiled, the concept of linking a dynamic link library and running a dynamic link library is different. during runtime, the dynamic link library of the program link must be in the system directory. There are several ways to solve this problem:
Place the directory of the Dynamic Link Library to the program search path. You can add the library path to the environment variable LD_LIBRARY_PATH:
Use the ld-Linux.so.2 to load the program, the command format is:
/Lib/ld-Linux.so.2-library-path Program name
Preface
Dynamic library loading is different from general dynamic library linking. Generally, dynamic library linking needs to find a dynamic library and find the library function when the program starts; the dynamic loading library can use a program method to control when to load. The dynamic library is mainly controlled by the functions dlopen (), dlerror (), dlsym (), and dlclose. The function prototype is as follows:
Minor discoveries:
Did you notice that we didn't use include
The cause of the following error is that the-lpthread option is not added to the compilation command.
If we add the-lpthread option, we try to remove the # include <pthread. h> in the program and recompile the program. You will find that the same is true.
Summary: if you are using functions in the standard library, you only need to include the header file. If the library where the function is located is not a standard library (for example, a library compiled by ourselves or a library extended outside the standard), you must add the-lxxx option during compilation, the header file can be included or not included, and the inclusion seems to be standard.