How to solve the link problem of dynamic library under Linux

Source: Internet
Author: User

about dynamic libraries and static libraries, here is not much to explain, how to create their own dynamic library and static library, previously mentioned, detailed content stamp links .


Static libraries are a way of exchanging space for time and portability, and in some cases it does make sense, but in some cases we write our own simple projects that do not have portability problems on most machines, and it is not necessary to use static compilation. Moreover, in some large projects, it is difficult to avoid the use of the Open Source Library, for example, if we simply refer to the MySQL database of several interfaces, it is necessary to complete the entire MySQL source files compiled? The price is too great. The usual practice is to put MySQL's include

And the Lib directory is packaged and released together to enhance portability, which is what is said here today in dynamic compilation.


However, by default, dynamic compilation will only go to the system default directory to find the lib***.so file, even if we use the-l option in makefile to indicate the Lib directory, compile without error, but at run time, the following error occurs:

./cgi_select:error while loading shared libraries:libmysqlclient.so.18:
Cannot open shared object File:no such file or directory


To solve this problem, there are a lot of methods on the Internet, not all listed here, give the most common method:

1, environment variables. Use the Export Import environment variable Ld_library_path to indicate the search path. This environment variable is temporarily specify the dynamic library search path, is bound terminal, for what reason, understand the Linux students should know, here not much explanation, in order to illustrate the problem, here, the specific usage is as follows:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/96/62/wKiom1kg-smzgGqsAABZaxT8YqE573.png "title=" 1.PNG "alt=" Wkiom1kg-smzggqsaabzaxt8yqe573.png "/>


Again, this practice is only temporary, in order to facilitate the use of the test, but the real project release, generally do not use this approach, because the need to create environment variables on the client machine to run, I tried a variety of practices, I want to put the command into the script Automation to execute, but limited to personal ability, The sub-shell has not yet crossed the pass.


2, the second approach is more effective. In the/etc/ld.so.conf file, the search path of the dynamic library is indicated, under the general paragraph, the file content is

Include ld.so.conf.d/*.conf

indicates that all *.conf files in the/ETC/LD.SO.CONF.D directory are saved in the search path of the dynamic library. That might not be a good idea. If you go to the directory and open a file, you will understand that there are only one or several paths in each file, as follows :


/usr/lib/qt-3.3/lib

/usr/lib/mysql

Each tool's dynamic library, has its own dynamic library path, so we can create their own *.conf file in the directory, the file to save their own specified dynamic library path can, individual absolute, Linux on the dynamic Library search path encapsulation do really good.

Of course, after you change other *.conf files or create your own dynamic Library connection directory profile, you need to use the Ldconfig command to make it effective.



3, the other way is some very non-mainstream practice, you need to put the dynamic library files directly into the system default path, frankly, this is not a problem, but this is to bury their own mines, sooner or later will be a big problem.


4, there is also a relatively uncommon practice, but open source projects will be used. The option to use GCC is really achievable,-wl-rpath specify the path, personally, rarely use this practice, gcc/g++ is a very powerful compilation tool, provides hundreds of options, there is a large part of the life can not be used, use to check on it, there is no need to dig too deep.



-----Muhuizz Finishing








This article is from the "Twilight Back" blog, make sure to keep this source http://muhuizz.blog.51cto.com/11321490/1927864

How to solve the link problem of dynamic library under Linux

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.