Add Dynamic link library path, dynamic library loading and other methods under Linux

Source: Internet
Author: User

Ways to add dynamic link library paths under LinuxJanuary 20, 2017 10:08:17Hits: 5596Linux Shared library Path configuration

A typical scenario where a shared library file cannot be found under Linux is that a soft package (such as Libnet,mysql) has been installed, and the compile connection works, but appears at run time as "error while loading shared libraries: Libnet.so.1:cannot open Shared object file:no such file or directory "error message.

The reason is that the shared library path under Linux is not configured correctly. The default Linux shared library search path is/lib and/usr/lib two directories (without subdirectories), and cannot be automatically connected if the shared library is not on these two paths (most typically/usr/local/lib).

There are five ways to solve this problem:

1. Use the static library (. A file) when connecting. Writes the full path of the. A file on the g++ command.

2, use the LD_LIBRARY_PATH environment variable. (temporarily valid, and sometimes no effect), add the path you want added to the Ld_library_path, and note that if more than one is separated by a colon. such as: Export Ld_library_path=/usr/local/lib/minigui

3, the dynamic library files are copied to the path that can be searched (usually the system default path, such as/lib,/usr/lib) inside, this is more ruthless, but may lead to some problems later.

4, compile the time set: When compiling the source code can use parameters:-WL,-rpath Specify the path of dynamic search.

5, modify the System file/etc/ld.so.conf, add the path, run the Ldconfig command.

The fifth method is described as follows:

The default dynamic-link library lookup path is specified in the/etc/ld.so.conf file, and the contents of my/etc/ld.so.conf file are include/etc/ld.so.conf.d/*.conf. That is, it indirectly specifies the file that defines the path, we only need to add the required path to the/etc/ LD.SO.CONF.D directory in any one of the files, and then run Ldconfig, but for easy to understand, it is best to find a relevant file, or to re-create a file, the need to add the path to write and then run Ldconfig

Ldconfig a few points to note
1. To/lib and/usr/lib inside add things, is not modified/etc/ld.so.conf, but after the end to adjust ldconfig, otherwise the library will not find
2. If you want to add things to the above two directories, be sure to modify the/etc/ld.so.conf, and then call Ldconfig, otherwise you will not find

Ps

1, Linux library files are divided into static libraries and dynamic library two kinds. Static libraries are accustomed to ending with. A, whereas dynamic libraries are accustomed to ending with. So (Shared object). And must start with Lib.

2, the principle of static library is "take space to change time", increase the program volume, reduce the running time;

Build: At compile time, Mr. Cheng is the target file. O, then archive the target file with an AR file and generate a static library file.

For example: AR-RC libtest.a myalib.o (note: AR-RC target. o filename), the target must start with LIB.

3, when using, in the link, plus the option-L after the library file name, note: must be the file name to remove the suffix and lib,
such as: Gcc-o main main.o-ltest. And the-ltest must be placed behind the MAIN.O, (the rule is that the lower the bottom of the library to be placed in the back).

4. Other common options for GCC,

-C compiled into a target file such as: Gcc-c MAIN.C is compiled main.c into the target file main.o

The find path of the-I header file, such as: Gcc-c main.c-i./inc means: The lookup path of the header file is in addition to the default, plus the./inc directory.

The lookup path of the-l library file, such as: Gcc-o main main.o-l./lib-ltest Description: libtest.a or libtest.so library file lookup path in addition to the default, plus the./lib directory.

-MM the dependencies of the exported file (with content in # include) such as: gcc-mm MAIN.C find the main.c dependent header file

-O to generate the final target

The-D macro defines the equivalent of some statement # define in C ... Example:-dpi=3.14 is equivalent to writing a statement in a file # define PI 3.14

5. Dynamic Library

(1), generated: In the link, with the following options:-shared-fpic: Gcc-fpic-shared-o libtest.so MYALIB.C

(2), use: implicit use and display use, implicit use is the sharing method, the program begins to run in the transfer. Use the following when linking:
Gcc-o main main.o-l./lib-ltest (like a static library)

Display use is in the program with a statement to the dynamic library redeployment, with the system call: Dlopen, Dlsym, Dlerror, dlclose functions, so when compiling links, do not add:-l./lib-ltest. However, to use the Dl* series function, add-ldl when compiling the link.

6, if the same directory, both static and dynamic libraries, such as LIBTEST.A libtest.so are present, then the DL program (and so on) on the dynamic library in the transfer, no dynamic, looking for static. No more, just an error.

7. Dynamic Library Search Path

The search paths for the DL to the dynamic library are as follows (in order)

A. The dynamic library search path specified when compiling the target code, (if you want to specify a program row in the./lib directory to find the library file libtest.so, the command is as follows: Gcc-o main main.c-l./lib-ltest-wl,-rpath./lib), of which,- The WL means that the following options are given directly to the LD program for processing, and the-rpath option is to change the search path to the following parameters./lib

B. Environment variable ld_library_path the specified dynamic library search path;

C. The dynamic library search path specified in configuration file/etc/ld.so.conf; (after modifying the file, update with Ldconfig)

D. Default dynamic library search path/lib and/usr/lib;

8. Some common commands (related to the library)

(1), LD is the link program of GCC.

(2), LDD is to view the executable file depends on the library's program, such as to check the main program used in those dynamic library, you can directly
LDD Main

(3), Ldconfig used to update the file/etc/ld.so.conf the changes take effect.

(4), NM is used to view the function name in the. So library, the tag is T is the name generated in the dynamic library. such as: nm/lib/libc*.so

Add dynamic link library paths under Linux, dynamic Library loading, and so on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.