Link and link options of the dynamic library-L,-rpath-link,-rpath

Source: Internet
Author: User
The link and link options of the dynamic library-L,-rpath-link,-rpath link how the program uses the shared library during connection, you must be able to find the location of the shared library during running. In linux, the executable program searches for the/lib and/usr/lib directories by default, and then... the link and link options of the dynamic library-L,-rpath-link,-rpath link how the program uses the shared library during connection, you must be able to find the location of the shared library during running. By default, the executable programs in linux search for the/lib and/usr/lib directories first, and then follow/etc/ld. so. the absolute path of the configuration search in conf. In addition, Linux provides the environment variable LDLIBRARYPATH for users to choose from. you can set it to find other paths except the default path, such as/work/lib path, you can go to/etc/rc. d/rc. add the following statement to the script that can be executed after local or other systems are started: LDLIBRARYPATH =/work/lib: $ (LDLIBRARYPATH ). In addition, the LDLIBRARYPATH path takes precedence over the default system Path (for details, refer to "use LDLIBRARYPATH"). However, LDLIBRARYPATH is set globally. excessive use of LDLIBRARYPATH may affect the running of other applications, so it is mostly used for debugging. (For LDLIBRARYPATH defects and usage principles, see Why LDLIBRARYPATH is bad.) We recommend that you use the-R or-rpath option of gcc to specify the library search path during compilation, and save the library path information in the executable file, when running, it will directly go to the path to find the library, avoiding the use of LDLIBRARYPATH environment variable search. The Link option and path modern connector separate the Link-time path and the Run-time path when handling the dynamic library, you can use-L to specify the path of the database to be connected, and use-R (or-rpath) to specify the path of the database to run the program. This greatly improves the flexibility of the database application. For example, when we perform embedded transplantation # arm-linux-gcc $ (CFLAGS) -o target-L/work/lib/zlib/-llibz-1.2.3 (work/lib/zlib is a cross-compiled zlib library ), after compiling target, we only need to copy the zlib library to the default system path of the Development Board. You can also use-rpath (or-R) and LDLIBRARYPATH to specify the search path. The options of the linker ld include-L,-rpath, and-rpath-link. after reading man ld, it roughly means-L: "link, the directory to be searched, that is, all the libraries in the-lFOO option, will be first searched from the directory specified by-L, and then the default location. The-L option during compilation does not affect the environment variable LDLIBRARYPATH.-L only specifies the path of the library during program compilation and connection, and does not affect the path of the library during program execution, the system will still find the library required by the program in the default path. if the library cannot be found, an error will still be reported, similar to cannot open shared object file. -Rpath-link: This is also used for "link". for example, if you want to display the specified FOO. so, but FOO. so itself requires BAR. so, you didn't specify the latter, but FOO. so references it. at this time, it will first find it from the path given by-rpath-link. -Rpath: directory to be searched during "running. During running, the. so file will be searched from the specified location in this option. For cross-compilation, the cross-compilation linker must have the -- with-sysroot option configured to take effect. That is to say, the path specified by-rpath will be recorded in the generated executable program and used to find the dynamic library to be loaded during runtime. -Rpath-link is only used for searching links. The link search sequence is man ld. The linker uses the following search paths to locate required shared libraries: 1. any directories specified by-rpath-link options. 2. any directories specified by-rpath options. the difference between-rpath and-rpath-link is that directories specified by-rpath options are encoded in the executable and used at runtime, whereas the-rpath-link option is only valid tive at link time. searching- Rpath in this way is only supported by native linkers and cross linkers which have been configured with the -- with-sysroot option. 3. on an ELF system, for native linkers, if the-rpath and-rpath-link options were not used, search the contents of the environment variable "LD_RUN_PATH ". 4. on SunOS, if the-rpath option was not used, search any directories specified using-L options. 5. for a nati Ve linker, the search the contents of the environment variable "LD_LIBRARY_PATH ". 6. for a native ELF linker, the directories in "DT_RUNPATH" or "DT_RPATH" of a shared library are searched for shared libraries needed by it. the "DT_RPATH" entries are ignored if "DT_RUNPATH" entries exist. 7. the default directories, normally/lib and/usr/lib. 8. for a native linker on an ELF system, if the file/ Etc/ld. so. conf exists, the list of directories found in that file. if the required shared library is not found, the linker will issue a warning and continue with the link. when using the ld link option in gcc, you need to prefix the option-Wl (the letter is l, not 1, I have made multiple mistakes ), the difference is not the compiler option. If the linker is being invoked indirectly, via a compiler driver (e.g. gcc) then all the linker command line options shoshould be prefixed by-Wl, (or whatever is appropriate for the particle compiler driver) like this: 1gcc-Wl, -- start-group foo. o bar. o-Wl, -- end-groupThis is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link.
Related Article

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.