Linuxc call Link Library-general Linux technology-Linux programming and kernel information. The following is a detailed description. I. call the static link library (. a) when calling a static link library, you only need to put the required static Link Library in the source file or. after the o file, you can compile, connect, or run it, you can also use the-L-l option to specify the Directory and name of the static link library (the-l usage rule is-lX, and the library used is libX. a) generally, they appear in pairs. Another important point is that the-L-l option is generally not placed at the end of the compilation command. Otherwise, an error occurs. II. dynamic Link Library call (. so) when using the dynamic link library to run the program, you need to add the-L-l option during program compilation to indicate the directory of the dynamic link library and that library. Once the program is connected, to use the shared library, you must be able to find the location of the shared library during running. By default, it is only found in the public library directory (for example,/lib,/usr/lib), so you cannot find your own dynamic library.
In linux, The excutable searches for the/lib and/usr/lib directories by default, and then follows the ld. so. the absolute path of configuration search in conf. By default, dynamic libraries are not searched in the current directory. Although the linux dynamic library search sequence can be said to be more rigorous, it is also relatively dull, sometimes causing inconvenience. In fact, linux also supports "loading dynamic libraries of other directories ". You only need to set the appropriate environment variable LD_LIBRARY_PATH. The setting methods include: 1. Temporary modification. After the log is out, it fails to be executed in terminal: export LD_LIBRARY_PATH = *** 2. Give the current account priority to load and modify the dynamic library in the current directory ~ /. Bash_profile adds two lines at the end of the file: LD_LIBRARY_PATH = *** and export LD_LIBRARY_PATH 3. Let all accounts load the dynamic library of the current directory first. Modify/etc/profile by adding two lines at the end of the file: LD_LIBRARY_PATH = *** and export LD_LIBRARY_PATHPS: Modify ld. so. conf can also be used, but only the following absolute paths are supported for Reprinted content, which is better written: When link library is used, there are two link Methods: static link and dynamic link. An Archieve is actually a compression set of Multiple object files. When you link, you specify an Archieve for the compiler. the compiler will extract the required object files and link them. (Static link) archieve can use the ar cr command to create (ar cr libtest. a test1.o test2.o) Note that the compiler will extract the required information from Archieve for the first time, the "required information" here is all the symbols that have been used but have not been defined. Therefore, it is very important to specify the order of Archieve in the command line, which is usually placed at the end. Suppose libtest. a defines the function f (), main. if the function f is used in o, the gcc-o app-ltest app is used. o will fail, because at-ltest, the compiler does not find any program calling the function f (), so it will not extract the information of f () from libtest. extracted from a and link o gcc-o app. o-ltest is compiled successfully. A shared library corresponds to archieve. The difference is that the shared library is not a collection of object files, but a single object file. Therefore, when the shared library is linked, the compiler will not link only the desired part of the library as archieve does, but all the content of the link. To create a shared library, you must specify the-fPIC option when compiling the object file with gcc. PIC indicates the Position-Independent Code, that is, the effect of the Code execution is irrelevant to the load address. Of course, in most cases, it is irrelevant. If you don't quite understand what I'm talking about, even if you ignore it, just remember to add-fPIC to build an object file for shared library. Then we can use gcc with-shared option to construct a shared library: gcc-shared-fPIC-o libtest. so test1.o test2.o. The suffix of shared library is so. The format of link shared library is the same as that of archieve when compiling executable files. Therefore, when linking, the compiler will first find the directory of each library (-L-specified first). If one of the shared library or archieve is found, link it. If both are found in the same directory, the compiler defaults to link shared library unless-static option is specified for gcc. The ldd command will display a shared library environment variable LD_LIBRARY_PATH on the executable file to indicate the library path when compiling the C file, gcc will automatically link the Standard C library and libc. When the C ++ file is compiled, g ++ automatically links the Standard C ++ library and libstdc ++. If you need to use some mathematical functions, such as sine functions, link libm library may be dependent, that is, one library calls the functions defined in another library, which is easy to understand. In this case, when using dynamic link, you only need to specify the library that the program directly depends on. However, when using static link, you need to specify all libraries that the program depends on, including the library for passing dependencies. In addition to link during compilation, dynamic link can also be used in the program, using dlopen (load library), dlsym (find the function in the library) and dlclose (unload library) if you use C ++ to write shared libraries, you should declare the function that requires export as extern "c". Otherwise, C ++ will rename the function to something that you cannot understand, and linker cannot understand.
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