Original post: http://soho-schubert.blogspot.com/2007/08/linux.html
The Linux programming library wrote a short section of MySQL C code yesterday. The compilation was successful, but the runtime report could not find the library. The system was FC4, and MySQL was directly downloaded binary:
# GCC test_mysql.c-O test_mysql-L/usr/local/MySQL/lib-lmysqlclient
#./Test_mysql: Error while loading shared libraries:
Libmysqlclient. so.15: cannot open shared object file: no such file or directory
Although the system does not find the path to load libmysqlclient. So, and can copy libmysqlclient. so.15 to/usr/lib, it does not know the principle and better method. So let's look at Chapter 10th of the gun/Linux Programming Guide (Kurt wall). The answer is as follows.
There are three methods to use a library in a non-standard location/usr/lib and/lib during runtime:
(1) set $ LD_LIBRARY_PATH = library directory (multiple directories are separated by:), the system load tool lD. So/ld-linux.so will search the Directories specified by the variable in sequence. For example, # $ LD_LIBRARY_PATH =/usr/local/MySQL/lib./test_mysql
(2) Add the library path to/etc/LD as root. so. conf or in/etc/lD. so. conf. d. CONF file, and then run ldconfig to update/etc/lD. so. cache. For example, create the mysql. conf file under/etc/lD. So. conf. D and write it to/usr/local/MySQL/lib.
(3) Another method is to copy the required library to/usr/lib or/lib, but this is not the recommended method. In particular, avoid sending the copy to/lib whenever possible. However, this method does not require the-L option during compilation.
The shared library search sequence is usually $ ldlibrary_path,/etc/lD. So. cache,/usr/lib,/lib
In addition, I also reviewed other basic knowledge related to the database:
The purpose of the library is to reuse code and provide shared functions. A program provides public interfaces for other programs.
Name and number:
(1) All databases start with Lib, and GCC automatically inserts lib before the file name specified by-L. For example, libmysqlclient uses-lmysqlclient
(2). A is a static library (archive), such as libmysqlclient.
(3). So is a shared object, such as libmysqlclient. So.
(4) Number Format: library_name.major_num.minor_num.patch_num, for example, libmysqlclient. so.15.0.0
(5) _ g and _ p:/usr/lib/libform_g.a indicate that this is libform. use the locate _ g. A will find many similar libraries, but I use locate _ g. so no such library is found in FC4; _ p in libxxx_p.a indicates that this is libxxx. a's performance analysis Library (profiling), but I use locate _ p. A and locate _ p. so no such libraries are found in FC4.
The library must be used with the interface header file. Common libraries include:
Libc. So (header files not required) Standard C library
Libdl. So (dlfcn. h) allows the program to load and use library code while running, instead of linking the library at compilation.
Libglib. So (glib. h) glib tool functions, such as hash and string
Libgthread. So (glib. h) supports glib threads.
Libm. So (math. h) Standard C math library
Libpthread. So (pthread. h) POSIX standard Linux thread Library
Libz. So (zlib. h) Common compression library
Database operation command:
(1) nm lists all symbols of the target file or binary file
(2) create static libraries and symbol indexes for Ar
(3) LDD lists the shared libraries required for normal program running, such
# LDD test_mysql
Linux-gate.so.1 => (0x00c59000)
Libmysqlclient. so.15 =>/lib/libmysqlclient. so.15 (0x009a1000)
Libc. so.6 =>/lib/libc. so.6 (0x0038b000)
Libpthread. so.0 =>/lib/libpthread. so.0 (0x004f8000)
Libcrypt. so.1 =>/lib/libcrypt. so.1 (0x002f0000)
Libnsl. so.1 =>/lib/libnsl. so.1 (0x00320000)
Libm. so.6 =>/lib/libm. so.6 (0x004bd000)
(4) ldconfig and the dynamic link together with the loading tool lD. So/ld-linux.so determine the link required for the so library located under/usr/lib and/lib. Ldconfig creates a symbolic link from the actual library to the so library name. Note:/etc/lD. So. cache,/etc/lD. So. conf ldconfig-P lists the Library Reference Links in/etc/lD. So. cache.
Dynamic linker lD. So/ld-linux.so uses some environment variables:
$ Ldlibrary_path: The format is similar to $ path, which is separated. It is a non-standard location/usr/lib and/lib library or/etc/lD. so. A database that does not exist in the cache. You must add this variable to search for the database.
$ Ld_preload: a space is separated to define the library to be loaded at the beginning. It can also be replaced by the/etc/lD. So. preload file.
Both the static library and the shared library contain objects.
Create and use static databases:
(1) Compile the code into the target file, such as gcc-C libxxx. C-o libxxx. O.
(2) Ar: Ar-RCS linxxx. A linxxx. o
(3) GCC-static: GCC test. C-o Test-static-L.-LXXX
(4) use file to check executable files with static links
(5) use the NM check symbol. The static link has no undefined symbol.
Shared libraries consume less system resources (disks and memory) and load a single file based on the shared link during running. This feature is fast and easy to maintain. At runtime, the LD. So/ld-linux.so links the symbolic names in the binary to the appropriate so library.
Create and use a shared library:
(1) GCC-FPIC generates location-independent code, such as gcc-FPIC-g-C libxxx. C-o libxxx. o
(2) GCC-shared and-soname, such as gcc-g-shared-wl,-soname,-libxxx. so-O libxxx. so.1.0.0 libxxx. O (note-wl,-soname,-libxxx. so there is no space in the middle)
(3) pass parameters to the linker LD through gcc-wl
(4) GCC-l explicitly link to the C library
During compilation, you can specify a non-standard location in/usr/lib and/lib by using the-l library directory.
Posting by Schubert Bobo time:03:02:00 labels: Linux, program development