Notes on "proficient in unix c language-programming and project practice"
1. Database category
The library can be divided into static library and dynamic library.
A. Static Library
The static library code is linked to the application during compilation. Therefore, the library file must exist during compilation and must be passed to the compiler through the "-l" parameter. When the application starts to run, the library function code will be transferred to the program memory segment along with the program until the process ends, and the execution process does not need to be static.
B. Dynamic library
The shared library does not link its code to the target file. Only when the dynamic library is accessed can the application correctly execute the dynamic library function. An application can execute dynamic library functions in two ways: implicit call and explicit call.
(1) implicit call
It is also called static loading of shared libraries. Dynamic library functions are automatically loaded into the memory at the beginning of the application, and are automatically uninstalled at the end of the entry. The compilation method of the implicit call is the same as that of the static library.
(2) explicit call
It is also known as dynamic loading of shared libraries. during compilation, the original dynamic library file and name can be provided without explicit support, but the call must follow the rules of functions such as dlopen.
Compared with other function calls, an application that uses the explicit call method can load or release the memory space occupied by the dynamic library during any running period.
The dynamic libraries that are implicitly called must be stored in a specific directory with a fixed name before they can be correctly executed. explicit calls do not have this restriction.
2. Static library operation tools
You can use the AR command to create or operate static databases in the following format:
Ar [drqtpmx] [Options] archivefile objfile ......
Archivefile is the name of the static library, and objfile is an intermediate target file name with ". O" as the extension, which can be used in multiple parallel. The following table describes the parameter meanings of "[drqtpmx]" "[Options.
-R inserts the objfile file into the end of the static library or replaces the file with the same name in the static library.
-X Extract files from static library files objfile
-T print the member file list of the static library
-D. Delete the object objfile from the static library.
-S: resetting static library file Indexes
-V Create File Redundancy Information
-C. Create a static library file
Enter:
# Ar-x/usr/lib/libc.
# Ls *. o
The extracted. o file is displayed.
Recreate the two. O files to the static library.
# Ar-r libmy. A a64l. O abort. o
You can generate the libmy. A file in the directory.
3. Static library Programming
The static programming library must exist during program compilation. Its code is linked to the application, and the application can be executed independently of the static library.
A. Static library generation and application example:
Write two files: pr1.c and pr2.c.
[Pr1.c]
Void print1 ()
{
Printf ("This is the first lib SRC! /N ");
}
[Pr2.c]
Void print2 ()
{
Printf ("this is the second lib SRC! /N ");
}
After compilation, compile these two files into. O files.
# Gcc-C pr1.c pr2.c
After the execution is complete, the pr1.o and pr2.o files are generated.
Link to a static database after creation
# Ar-respiratory libmy. A pr1.o pr2.o
Libmy. A is generated after execution.
Then you can program to call the library function.
[Main. C]
Void main ()
{
Print1 ();
Print2 ();
}
There are two ways to compile
# Gcc-O main. c-l. /-lmy //-L specifies the directory of the library file to be searched.-lmy indicates calling libmy. database A in the format of-L [name]
Or
# Gcc-O main. C./libmy.
4. Dynamic library Programming
Dynamic library generation can be divided into three steps: Design Library source code, build location-independent code type. o file and link dynamic library.
A. Dynamic library generation and calling
First, design the source code of the library.
[D1.c]
Int P = 2;
Void print ()
{
Printf ("This is the first dll src! /N ");
}
[Pr2.c]
Int P = 3;
Void print ()
{
Printf ("this is the second dll src! /N ");
}
There are two methods for compiling to a database:
# Gcc-FPIC-C d1.c d2.c // create a. o file
# Gcc-shared-O d1.so d1.o // create so
# Gcc-shared-O d2.so d2.o // create so
Or
# Gcc-FPIC-shared-O d1.so d1.c // directly create so
# Gcc-FPIC-shared-O d2.so d2.c // directly create so
After d1.so and d2.so are generated, you can write a program to call them.
Compile the main program
[Main. C]
Void main ()
{
Print ();
}
1. implicit call of the dynamic library
# Cp d1.so DLL. So
# Gcc-O main. C./DLL. So
After overwriting DLL. So with d2.so, the running results are different.
2. Dynamic library display call
[Main. C]
# Include <dlfcn. h>
Void main ()
{
Void * phandle;
Void (* pfunc )();
Int * P;
Phandle = dlopen ("./DLL. So", rtld_now); // open the dynamic library
If (! Phandle)
{
Printf ("cann't find DLL. So/N ");
Return;
}
Pfunc = (void (*) () dlsym (phandle, "print"); // void (*) () is a print type pfunc get library function address
If (pfunc)
{
Pfunc ();
}
Else
{
Printf ("cann't find func print/N ");
}
P = (int *) dlsym (phandle, "P"); // call the variable in the dynamic library
If (P)
{
Printf ("P = % d/N", * P );
}
Else
{
Printf ("cann't find int P/N ");
}
Dlclose (phandle );
}
Compile the file just written
# Gcc-O main. C-LDL
You can.