Go to: GCC header file and library file search path

Source: Internet
Author: User

Original article:

Http://blog.csdn.net/diantouxiao/article/details/5836345

References: http://hi.baidu.com/relayon/blog/item/95aaf7fcf8e3edf5fc037f89.html

 

We use three things when writing a program: header files, library files when linking, and dynamic library files when running. In the above 3, I think the header file is a preprocessing process and should be related to the compiler, while the library file is of course related to the linker at the link, the dynamic library file is related to the dynamic loader.

When creating a program:

Path search for header files;

1. Start with-I. This is specified during compilation.

2. GCC environment variables c_include_path, cplus_include_path, and objc_include_path (these variables correspond to include environment variables in Windows)

3. Then set the Directory:/usr/include/prefix/include wait (prefix is the directory during our installation). You can view it through gcc-print-search-dirs.

Path search for library files during connection;

1. First, find the-l directory specified during compilation. If it is not the default link library file, we also need to specify the-L option.

2. GCC environment variable LIBRARY_PATH

3. Find the built-in directory/lib:/usr/local/lib: It is written into the compiler search path when making the compiler. You can view gcc-print-search-dirs.

When running the program:

Of course, our program is ready now, but most of the current programs use dynamic libraries, dynamic libraries include dynamic libraries that are loaded when the execution starts and when the running program is required. Here we will only talk about the search path of the former. We may have encountered the problem that the dynamic library could not be found when running the program. There are dynamic libraries in Linux and Windows, which may be that the dynamic library required by the program does not exist, or, it is not placed in the specified position, so it cannot be found.

The search path of the dynamic library during runtime;

1. Specify the dynamic library search path in the configuration file/etc/lD. So. conf.

2. Specify the dynamic library search path through the Environment Variable LD_LIBRARY_PATH (when multiple dynamic library search paths are specified through this environment variable, the paths are separated by colons) (The HP-UX corresponds to the shlib_path environment variable, AIX corresponds to the libpath environment variable, Windows corresponds to the Lib environment variable)

3. Specify the dynamic library search path of the program when compiling the target code (you can also specify the dynamic library search path of the program when compiling the target code. This is specified through the GCC parameter "-wl,-rpath. When multiple dynamic library search paths are specified, the paths are separated by colons (:).

4. Default dynamic library search path/lib/usr/lib

The search path sequence of a dynamic library is as follows:

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

2. The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH;

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

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

After analysis, you probably know the relevant path search, so now you have to put the compilation tool or the thing required by the running tool in their search path, of course, if you consider the execution efficiency, you can also put it in the first search path in the search order. (In terms of the path search sequence, we also find that the principle is "user first", that is, to find the default directory from the directory specified by the user and then find another one .) However, this is troublesome. For example, when we dynamically run the program, we generally modify ld_liabrary_path or the-I-l option when compiling the link program.

Other same content article: http://jianlee.ylinux.org/Computer/C%E5%92%8CGNU%E5%BC%80%E5%8F%91/gcc.html#sec1

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.