[Linux] Linux library file write · entry;

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

One, why to want to use the library file

  We will certainly encounter this kind of situation in the actual programming: there are several projects have some function modules of code is the same, the same, repetitive code is also what we call. For example, many projects have a user authentication function.
code segments are as follows:

 

 

 

//UserLogin.h file, to provide a function declaration
int IsValidUser(char* username, int namelen);
//UserLogin.c file, to verify the user information
int IsValidUser(char* username, int namelen)
{
  
int IsValid = 0;
  
/*Below is the processing code specific, omit*/
  return IsValid
}

 

View Code

  If each item is preserved in the two UserLogin.h and UserLogin.c files, there will be several drawbacks:

  1. Every project has repeated module, resulting in duplication of code.
  2. Code reuse is not good, once the IsValidUser code has changed, in order to maintain the consistency of design, we have to manually modify the other items in the UserLogin.c file, which is time-consuming and laborious, but also prone to error. Library file is to the public code of a form.

  In order to solve the above two problems, put forward the solutions with the use of database files stored public code, the key is the public (which can be repeatedly reused) separate the target code from the project, unified stored in the database file, a project to use the code, object code can be obtained from the library files when compiling or running. Library file is divided into two kinds: static and dynamic libraries.

Two, the static library and dynamic library

  If the program is loaded libraries at compile time, is the use of a static library. If it is loading the target code at run time, becomes a dynamic library. In other words, if you are using a static library, the static library code at compile time, copy to the code segment program, program volume expansion. If the use of dynamic link library, the process to retain only the name and the name of the function library file, to find the library file and the body of the function at run time, volume change program is.

 

Three, write a static library and use

1,Summary

  The extension of static library file is.A, its preparation process is simple.

  1. Write a function code
  2. The compiler generates the target file
  3. The target file filing with the AR file, generating static library file.

  Note the archive file name must begin with Lib.

  Use points:

  1. The path in the -I parameters of GCC with the static library header file.
  2. The parameters in the -L after GCC library file and directory
  3. A library in the -l parameter of GCC plus, but to remove the lib and the.A extension.

  Such as the database file name is libtest.a then the parameter is -l test

 

2,Static library files of the most simple

  Write the following two documents, pay attention to put in the same directory

 

 

 

//The contents of the lcw_lib.h file
void test();

//The contents of the lcw_lib.c file
#inlcude <stdio.h>
void test()
{
  printf(
"lcw_lib test ! \n");
}

 

View Code

 

3,Making library file

⑴Generate an object file

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -c lcw_lib.c
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h lcw_lib.o

 

  After the implementation will generate a lcw_lib.o file

⑵Use the AR command file, the format for the AR -rc <generating file file name > <.O file list>

  Again, archive file name must begin with lib,.A.

 

mystery@lcw:~/Desktop/code/lib_test$ ar -rc libtest.a lcw_lib.o
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h lcw_lib.o libtest.a
mystery@lcw:
~/Desktop/code/lib_test$

 

   After the implementation will generate a libtest.a file

 

4,Use the library file

⑴Write a test program main.c

 

 

 

//Main.c static library calls the program
#include "lcw_lib.h" //The function header files included, or compile time error
int main(int argc,char* argv[])
{
  test();
  
return 0;
}

 

View Code

⑵Compiled object file, pay attention to the path to -I parameters of static library header files.

 

mystery@lcw:~/Desktop/code/lib_test$ gcc  -I `pwd` -o main.o -c main.c
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h lcw_lib.o libtest.a main.c main.o
mystery@lcw:
~/Desktop/code/lib_test$

 

  Now create a main.o file.

⑶Generation of executable files, pay attention to the path to the -L parameters of static library file, the database file name (remove the head lib and the end of the.A) is added to the -l parameter.

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -o main -L `pwd` main.o -l test
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h lcw_lib.o libtest.a main main.c main.o
mystery@lcw:
~/Desktop/code/lib_test$

 

  This will generate an executable file named main.

⑷Execute the executable file to see effect

  Execution./main

 

mystery@lcw:~/Desktop/code/lib_test$ ./main 
lcw_lib test
!
mystery@lcw:
~/Desktop/code/lib_test$

 

  OK,Test the success of.

 

Four, write the DLL

1,Summary

  Dynamic library generally end in.So, is shared object.
generation step for

  1. Write a function code
  2. The compiler to generate dynamic library file, with -shared and -fpic options, database file name starts with lib, ends with.So.

  Methods of using are divided into two types: implicit call and display call

  1. The use of implicit invocation is similar to the static library, but need to modify the dynamic link library file/etc/ld.so.conf;
  2. To display the call is the use of dlopen, in the main program in dlsym, dlerror, dlclose system function.

2,The most simple programming dynamic library file

  For ease of reference, we still use the static library file as an example.

 

mystery@lcw:~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h lcw_lib.o libtest.a main main.c main.o
mystery@lcw:
~/Desktop/code/lib_test$ rm main.o main libtest.a lcw_lib.o
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h main.c
mystery@lcw:
~/Desktop/code/lib_test$

 

 

3,The compiler to generate dynamic libraries, library file name starts with lib, ends with.So.

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -fpic -shared -o libtest.so lcw_lib.c
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h libtest.so main.c
mystery@lcw:
~/Desktop/code/lib_test$

 

  Creates a libtest.so file at.

 

Five, implicit dynamic library calls

  Implicit invocation is the meaning of the library file name does not appear in the code, the code and call the static library code is similar.

1,Write the test file

  //Main.c dynamic library implicit invocation procedure, as before

2, Compile the test program, similar to a static library, a path to a -I file inside his head to the parameters

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -I `pwd` -o main.o -c main.c 
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h libtest.so main.c main.o
mystery@lcw:
~/Desktop/code/lib_test$

 

  Now create a main.o file.

3,Generate test procedure.

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -o main -L `pwd` main.o -l test
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h libtest.so main main.c main.o
mystery@lcw:
~/Desktop/code/lib_test$

 

  Now create a main file.

4,Perform the test program

 

mystery@lcw:~/Desktop/code/lib_test$ ./main 
.
/main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
mystery@lcw:
~/Desktop/code/lib_test$

 

  Error, the reason is that the program does not know path dynamic libraries are not found, so the natural. The solution is as follows.

 

Six, the three ways of dynamic libraries are sharing system

  (1)Copy the dynamic link library to share system directory, or the directory for the dynamic link library to establish the connection sharing system (hard wired or symbols can be connected, the commonly used symbol connected). Here the system shared directory, refers to the dynamic link library LINUX directory, including lib, a series of catalogue /usr/lib and in the /etc/ld.so.conf file.

Example: the implementation

 

# cp libtest.so /lib
# ldconfig
Or:
#
ln -s `pwd`/libtest.so /lib
# ldconfig

 

Note two a backquote ` before and after PWD, its purpose is to make the output of the PWD command, which is the current directory. Then the implementation of main, can be successful.

  (2)The dynamic link library directory name is appended to the dynamic link library configuration file /etc/ld.so.conf.

 

# pwd >> /etc/ld.so.conf
# ldconfig

 

 

Then the implementation of main, can be successful.

  (3)The use of dynamic link library management command ldconfig, forced to search the specified directory, and update the cache files, for dynamic loading.  

 

# ldconfig `pwd`

 

Then the implementation of main, can be successful.

  Should pay attention to, although effective in third ways, but the effect is temporary, for program testing can also run ldconfig, once again, the cache file content may change, the dynamic link library required may not be sharing system.

  Either way, its essence is to use the ldconfig command to the dynamic library file where the path is added to the system library list, (the first two permanent, third temporary)

 

Seven, explicit dynamic library calls

  Explicit call is the meaning of code database file name, users need to open and manage library file . The main points of:

  1. The dlfcn.h system header files included in
  2. Open the library files with the dlopen function, and the specified open

  dllopeThe first parameter is the shared library name, will be in the shared library to find specified below.

  • The environment variable LD_LIBRARY_PATH lists for all directories semicolon interval.
  • List to find the file in /etc/ld.so.cache database, updated by the ldconfig command.
  • The directory usr/lib.
  • Catalog/lib.
  • The current directory.

  Shared library mode parameters for the second open. There are two value

  1. RTLD_NOW: Will share all the function to load the library into memory
  2. RTLD_LAZY: Push back functions in the shared library load operation, until the call dlsym () when loading a function
  3. Using dlerror () function to test whether the open succeeded, and error handling;
  4. To get the address of the function with dlsym, stored in a function pointer
  5. A function call with the obtained function pointer.
  6. At the end of the program by using dlclose dynamic library open close, prevent resource leaks.
  7. Using the ldconfig tool path is added to the list of system dynamic library.

1,Write the test file

  //Main.c dynamic library explicitly call program

 

 

 

 1 #include<dlfcn.h>      //The header file for system dynamic library management
2 #include "lcw_lib.h" //The function header files included, or compile time error
3 int main(int argc,char* argv[])
4 {
5 //Declare function corresponding to the function pointer
6 void (*pTest)();
7 //Load dynamic library
8 void *pdlHandle = dlopen("libtest.so", RTLD_LAZY);
9 //Error handling
10 if(pdlHandle == NULL )
11 {
12 printf("Failed load library\n");
13 return -1;
14 }
15 char* pszErr = dlerror();
16 if(pszErr != NULL)
17 {
18 printf("%s\n", pszErr);
19 return -1;
20 }
21 //Gets the address of the function
22 pTest = dlsym(pdlHandle, "test");
23 pszErr = dlerror();
24 if(pszErr != NULL)
25 {
26 printf("%s\n", pszErr);
27 dlclose(pdlHandle);
28 return -1;
29 }
30 //Realization of function calls
31 (*pTest)();
32 //Close the dynamic library at the end of the program
33 dlclose(pdlHandle);
34 return 0;
35 }

 

View Code

 

2,Compile the test file using the -ldl option specified produces object modules require the use of a shared library

 

mystery@lcw:~/Desktop/code/lib_test$ gcc -o main main.c -ldl
mystery@lcw:
~/Desktop/code/lib_test$ ls
lcw_lib.c lcw_lib.h libtest.so main main.c
mystery@lcw:
~/Desktop/code/lib_test$

 

  After the implementation will generate a main file, I parameters of -ldl need to be placed in the final, is placed in front of you can not

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by April at November 14, 2013 - 9:59 AM