How to use static library and shared library on Linux platform

  

1, Linux function library introduction

The function library can be regarded as a set of functions written in advance, which can be separated from the main function, thus increasing the program. Reusability of development. Function libraries in Linux can be used in three forms: static, shared, and dynamic.

1) The code for the static library is connected to the developer-developed application at compile time;

2) and the shared library is only loaded when the program starts running;

3) The dynamic library is also loaded when the program is running, but unlike the shared library, the library function used by the dynamic library is not loaded in the program, but the statement in the program needs to use the function. Only loaded. Dynamic libraries can free up memory used by dynamic libraries during program execution, freeing up space for other programs to use.

Note:

l Since shared libraries and dynamic libraries do not include the contents of library functions in the program, they only contain references to library functions, so the size of the code is relatively small.

l Most of the libraries available in the system are stored in /usr/local/lib, /usr/lib, /lib, and directories.

l Most of the header files are placed in the /usr/include, /usr/local/include directory.

l The configuration files and management commands of the shared library are as follows:

/etc/ld.so.conf: The search location of the shared library.

ldconfig: Shared library management tool, generally run this command after updating the shared library.

ldd: You can view the shared libraries used by the executable.

l The library file name consists of the prefix lib and the library name and suffix. Depending on the type of the library, the suffix name is different.


2, the main reason for the extensive use of library files

1) For the convenience of programming, for some commonly used functions, the corresponding development language provides a corresponding library File support.

2) Hide specific function implementation details. When a programmer uses a library file, he only needs to include the header file of the required function, without having to care about the specific implementation of the function. For commercial software, its intellectual property is also protected.


3. What are the ways to use these library functions?

In any programming environment, library files are collections of precompiled functions. These functions are in binary. The code form is stored in a library file. To use these functions, users only need to include these library files. In general, there are two ways to get the corresponding function from the library file.

1) At the time of compiling, the binary image code of the corresponding function in the library is directly copied to the currently compiled program, and the current program is run independently. This library is called a static library. In Linux, the .a suffix is ​​a static library.

2) At the compile time, only the entry address of the binary image code of the corresponding function in the library (not directly copied) is referenced. The program reads the function code from the shared library file at runtime (this needs to be first The shared library is loaded into memory), which indirectly references. This library is called a shared library. In Linux, it is named in the format libxxx.xo.xx.


4, how to use

The three types of methods using static library, shared library and dynamic library are very similar, all use the option is “-l” (Note that this is lowercase "L”). This option is used to indicate the specific library file to use. Since the naming rules of the function library in Linux start with "ld"; lib”, the library file here only needs to fill in the content after lib. For example: there is a static library file libm.a, just write "&lm";-lm”; also for the dynamic library file libm.so; when writing, you only need to write “-lm”, the whole call command Similar to the following:

[root@localhost gcc]# gcc -o dynamic –L /root/lq/testc/lib/dynamic.o -lmydynamic

So, if both systems exist When a static library file and a dynamic library file with the same file name are used, does the link option call a static library file or a dynamic library file? After testing, you can find that the system calls the dynamic library file, which is because the default in Linux system is to use dynamic linking. In this way, if the user wants to call a static library file containing a dynamic library file with the same name, it is necessary to display the file name containing the suffix name after “-l”, for example, to write the libm.a library file, you need to write &ldquo ;-llibm.a”.


1) If you already have a static library file libhello.a, the header file for this library file is libhello.h, which has the following contents:

#ifndef __libhello_H__

#define __libhello_H__

void print_hello(void); //print hello world,this is library to console

#endif /*__libhello_H__*/

We can see from the above code that the header file knows that the libhello.a library file contains the print_hello() function, and its return value type and parameters are empty. Therefore, when writing a program, you only need to use this function (with the header file included), regardless of its implementation details.

The sample program main.c using the libhello.a library file is as follows:

#include "libhello.h"

int main(int argc, char** argv )

{

print_hello(); //Reference library function

return 0;

}

We can now put main .c, libhello.a copy to the same directory, compile:

#gcc –o usehello_static usehello.c –lhello //You can also use libhello.a directly, without adding a horizontal bar

2) If you already have a shared library file libhello.so.0.0, we have to do two symbolic links here (the specific reason I am not very clear):

# ln – s libhello.so.0 libhello.so.0.0

# ln –s libhello.so libhello.so.0

We can copy these three files to the current directory, this library The print_hello() function is defined in the file libhello.so.0.0. You can view its header file libhello.h, which contains the following contents:

#ifndef __libhello_H__

#define __libhello_H__

void print_hello(void); //that is, print_hello( The function has been alive

#endif /*__libhello_H__*/

Similarly, we can also reference the function in the shared library file in main.c, the main.c content is as follows:

#include "libhello.h"

int main(int argc, char** argv)

{

print_hello(); //Reference library function

return 0;

}

Then compile the connection with the following command (you can copy the library file libhello.so.0.0 to the current directory or system /lib Under the directory):

#gcc –Wall –g –c usehello.c –o usehello.o //usehello.o is a binary object file

# gcc –g –o usehello_dynamic usehello.o –L ./-lhello //The red tag is a command specifically for referencing shared libraries”-L dir”

After generating the executable file, you can use ldd The shared library used by the command executable:

# ldd usehello_dynamic

The terminal will prompt: libhello.so.0 => not found, so if you want this executable to run as expected, you must It runs the shared library to the library's search path:

# LD_LIBRARY_PATH = $(pwd) ./usehello_dynamic

Copyright © Windows knowledge All Rights Reserved