1. Create your own dynamic and static libraries
In Linux, the dynamic library. the static library ends. end with A. They all start with Lib. For example, if a library name is net, its full name should be Libnet. so or Libnet. a.
We have two files, hello. C and test. C. The content of the two files is as follows:
// Hello. c
# Include <stdio. h>
Void my_lib_func ()
{
Printf ("libraryroutine called \ r \ n ");
}
// Test. c
# Include <stdio. h>
Int main ()
{
My_lib_func ();
Return1;
}
Test. c calls the hello. C method. We encapsulate hello. C as a library file. Both static and dynamic libraries are composed of. O files.
-Chello. C: generate the. o file
Create a static library
Ar CRV libmyhello. A hello. O, Ar is the command to generate the static library, and libmyhello. A is the name of my static library. The next step is to use the static library in my program.
The result of libraryroutine called is displayed, indicating that the call is successful.
Next we will delete libmyhello. A to see if the program is still running normally.
We found that the program is still running normally, indicating that the static library has been connected to our program.
Create a dynamic library
We can see that the dynamic library libmyhello. So has been generated. Continue to use
The library file cannot be found. At this time, we copy the so file to/usr/lib.
Running successful
2. Call rules for both dynamic and static databases
We can find that both the dynamic library and the static library are added with the-l parameter during program compilation and connection, so when they exist at the same time, the program will choose dynamic library or static library. Let's make a try.
Libmyhello. A And libmyhello. so, we found that the dynamic library could not be found during the running, so we can draw a conclusion that, when both the dynamic library and the static library exist, GCC will give priority to the dynamic library.