When writing a program, the dynamic library is often used tools, in the context of the VS programming environment only need to complete the project localization configuration or directly to the environment variables can be configured (not recommended, after all, a fuss), and for the landlord like this just contact with the Linux operating system rookie, Configuring the dynamic (shared) library is not yet a simple issue.
Before introducing the call method of the dynamic library, let's introduce the compilation of the dynamic library (how to generate the so file)
You need a header file with several method declarations and a corresponding definition file. For example
//so_test.h 头文件#include "stdio.h"void test_a();void test_b();void test_c();
//test_a.c:#include "so_test.h"void test_a(){ printf("this is in test_a...\n");}
//test_b.c:#include "so_test.h"void test_b(){ printf("this is in test_b...\n");}
#include "so_test.h"void test_c(){ printf("this is in test_c...\n");}
Place the above so_test.h test_a.c test_b.c test_c.c file with the same directory and perform
gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so
At this point, we can see an extra libtest.so file, where the naming is somewhat fastidious, should be named after lib+xxxx+.so and then explain why, this is the shared library file we just generated.
-fpic: represents compiled to a location-independent code , without this option, the compiled code is location-dependent, so the dynamic loading is a code copy of the way to meet the needs of different processes, but not to achieve the purpose of real code snippet sharing (as if not necessary OH)
-shared: This option specifies to generate a dynamic connection library, which is required
To come here seems to have finished writing the shared library, so let's start with a brief introduction to the next two types of calls:
① Implicit invocation
② Display Call
When it comes to simplicity, implicit invocation means that in our program code, there is no definition of which shared library we have called, just a header file that contains a contribution library, and a shared library that needs to be specified when compiling. Let's continue with the example description:
#include "so_test.h"int main(){test_a();test_b();test_c();return0;}
Very simple call, and the normal link is no different ah! Yes, but it takes a bit of effort to compile.
gcc test.-L.-ltest-o test
-L.: Indicates that a shared library needs to be linked in the current directory;
-itest: The file name that represents the shared library that is called lib+test+.so is libtest.so
Let's run the generated test file
Cannot open Shared object file
Why not, don't worry. We use the LDD command to see the link to the test file
text
We just wrote the libtest.so and you put in a folder actually said can't find, served (: Зゝ∠)
In other words, the link library can be retrieved from the following five locations
① environment variable Ld_library_path all directories listed with a semicolon interval;
② the list of libraries found in file/etc/ld.so.cache, refreshed by the ldconfig command;
③ directory usr/lib;
④ directory/lib;
⑤ current directory;
Do I recommend that you copy the shared libraries we generate to the/usr/lib/or/lib directory, and that modifying Ld_library_path is not as dramatic as changing environment variables in the Windows environment? (Personal opinion please the Great God)
cp libtest.so /usr/lib/
Now we find that he can find the shared library we wrote, and it will work.
Okay, now look at the display call
At this point we have written the Libtest.so shared library and copied it to the/usr/lib/directory
The call to show is to explicitly indicate in code the shared library we need to call, without having to specify it at compile time, so we need some special functions to do the above functions.
We need to use a header file Dlfcn.h and its corresponding four functions. The following describes the specific steps to be explained.
① include the Dlfcn.h system header file.
② Open the library file with the Dlopen () function and specify the open mode;
Dllope () has two parameters
The first parameter is the name of the shared library, and the location that can be taken is not described.
The second parameter is how you open the shared library. There are two of values
①rtld_now: Load all functions in the shared library into memory
②rtld_lazy: The load operation of a function in the shared library is pushed off until a function is loaded by calling Dlsym ()
The return value is a void * pointer
③ uses the Dlerror () function to test whether the open succeeds and error handling, and the return value is a char *;
④ uses Dlsym to obtain the address of the function, stored in a function pointer, the calling format is Dlsym (void *, "fcnname");
⑤ function calls with the obtained function pointers.
⑥ closes the open dynamic Library with dlclose (void *) at the end of the program to prevent resource leaks.
#include <dlfcn.h> //Include header files #include "so_test.h" //contains shared library header files intMainintargcChar*argv[]) {void(*PT) ();//apply for a function pointer corresponding to the shared library void*handle=dlopen ("Libtest.so", Rtld_lazy);//Open Shared library, and have void pointer handle save if(Handle==null)//Detect if success is turned on{printf("Fail load library\n");return-1; }Char* Pe=dlerror ();//Detect if the function is open successfully if(Pe!=null) {printf("%s\n", PE);return-1; } pt=dlsym (Handle,"Test_a");//Call a specific functionPe=dlerror ();//Detect again if the function is open successfully if(Pe!=null) {printf("%s\n", PE);return-1; } (*PT) ();//functions that call shared librariesDlclose (Handle);//Close shared library return 0;}
The Compile command is
-o-ldl test.
-LDL: Indicates that the build object needs to use a shared library, but does not specify which shared library
(^o^)/~ call dynamic (Shared library) the first way to talk about this!!
How to use dynamic libraries