Run the compiled program error error while loading shared Libraries:lib*.so:cannot open Shared object file:no such file or directory

Source: Internet
Author: User

  Run the compiled program error error while loading shared Libraries:lib*.so:cannot open Shared object file:no such file or directory

--------------------------------------------------------------------------------------------------------------- ----------------------------------

First of all, this topic is a bit long, but it is a good response to the problems encountered.

In the "How to generate two-dimensional code with C code" in the article, the small part also shows you in the correct compilation of the Qrcode_test program, run an error problem. Let's talk about it again this time.

At that time at the Terminal Manual./Run the program to indicate an error ./qrcode_test:error while loading shared libraries:libzint.so.2.4:cannot open Shared object File:no such file or directory

The idea is that when you run the program, you don't find the libzint.so dynamic library that the program needs. As we all know, dynamic library and Static library is the biggest difference is: Static library is static link , that is, in the production of executable files at the time of the static library embedded in the program, once compiled successfully, the static library has the value of existence, even if the static library does not exist, Executable program can also run up, but the dynamic library is not the same, it is to follow the dynamic link , it is said that the compile time needs to specify the path to find the so file link compilation, run time also need to specify the appropriate path to find. If run, the executable program will go to the default system Lib directory, look for the so, if not found, it should be error: Error while loading shared libraries. (This article can be used to understand the dynamic loading mechanism of the dynamic library http://blog.csdn.net/dbzhang800/article/details/6918413)

Above this paragraph, say executable program run to find so file, speak more general, the following summary:

The order of Search path search for dynamic libraries is:

  1. The dynamic library search path specified when compiling the target code;

2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path;

3. The dynamic library search path specified in the configuration file/etc/ld.so.conf;

4. The default dynamic library search path/lib/usr/lib.

Refer to this blog post, which mentions GCC's knowledge of compiling dynamic libraries and running linked dynamic libraries. http://www.cnblogs.com/zhengmeifu/archive/2010/03/02/linux-gcc_compile_header_file_and_lib_path.html

Back to the executable program run the report error while loading shared libraries error, in the "How to generate two-dimensional code with C code" article, also talked about how to avoid this error and run the program, we use the above mentioned " 2. environment variable ld_library_path Specify the Dynamic library search path, "That is, by setting the Ld_library_path, the current program uses the path of so to add to the Ld_library_path, so when the program runs up, go to Ld_library_ Path is sure to find the so. Of course, we have verified the correctness of this method before.

See, the program runs without problems.

If the method is only repeated, then the meaning of this article is not much. The biggest implication of this article is that we can tell the reader that we don't have to set up Ld_library_pat, and we can run the program, so where do we start? This article is mainly about, starting with compiling the program.

This is the first to introduce the GCC compilation options, but here is not specific, interested in the man gcc to understand, or from the post of the previous article "reprint" GCC used in the use of parameters and commands http://www.cnblogs.com/Recan/p/ 6012248.html to understand.

Here, the compile option we need to use is-wl,-rpath; this is mainly the letter L, not the number 1. The-rpath option is to tell GCC to write to the elf file the path that the program runs to find so when compiling the link. The use method is Gcc–o outapp *.c–lzint–wl,-rpath= "/usr/local/lib" or Gcc–o outapp *.c–lzint–wl,-rpath–wl, "/usr/local/lib" both functions are Equivalent. As shown in the following:

As we can see, it is not possible to run the program directly after the compilation is successful and the error while loading shared libraries, and the greatest benefit of this method is that, after the successful compilation, you can run the program without having to do any setup. Of course, the-rpath parameter passed at compile time is correct. This method, especially when cross-compiling an executable program for an embedded device, is useful. Readers can try it out for themselves.

Run the compiled program error error while loading shared Libraries:lib*.so:cannot open Shared object file:no such file or directory

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.