Environment variable, include search path, lib library search path

Source: Internet
Author: User

Link: http://blog.sina.com.cn/s/blog_93b45b0f01011nrz.html

Environment Variable

 

System Environment Variables

 

We know that we often need to set some environment variables, which are very easy to understand. In fact, we often have access to windows. In fact, environment variables are a very broad concept, which is very similar to the roles of web. config in web applications. What does it mean? That is to say, when a program (system or application) is to run, its basic business logic may be certain, but there are many configurable things that can change the program when implementing the business logic. For example, after a web application is compiled, its business logic will basically not change. However, if you change some web. config parameters, the running of the program will change accordingly. These settings. Just like modulation on a TV. If you change the settings, some differences may occur.

So the environment variables can be understood as one of the settings. Why is it not directly called the settings? Because it is in a passive situation. The more you say, the more confused you are.

The most common environment variables are PATH and ClassPATH. You need to set them when setting jdk. the PATH variable here refers, when the system interface receives a program startup command, in addition to finding the executable file in the current directory by default, it also needs to look for the location. With this setting, you do not need to enter that directory to execute that program. The ClassPATH variable is similar. It sets paths similar to dynamic libraries. That is to say, when the program is executed, it finds that the dynamic library should be introduced, in this case, you need to find it at the place specified by this variable.

In linux, the system also has a PATH variable. In fact, the system has a file dedicated to recording those environment variables.

1)/etc/profile. The system will log on and execute this file to introduce those variables in the current environment.

2) andThe/home/ali/. bashrc file, in simple terms,/etc/profile is globally valid, and./bashrc is valid for the current user.

3) Another setting method is to directly modify the file by using terminal commands. We know that the method used by the previous two files is that when the program enters the state, they will be introduced into the current space by execution, so these variables will be available in the current state, and the program can use them. So if we modify them directly in the memory, it can play a temporary role.

 

Program environment variables

 

According to the role and significance of environment variables, it is easy to launch. Common programs can also have environment variables. Follow the previous system environment variable action mode. Applications can also have some configuration files to persistently save these environment variables. During program execution, these variables will enter the space for program execution in some way, in this way, these variables can be used during program execution. In the same way, we can change these variables to change our program in the right way.

GCC is such a program. Most of the time, applications do not need the environment variable mechanism. The key is to check whether there are many alternatives. GCC is such a program.

The most common GCC environment variables are include search paths and library search paths. They are used for compilation and connection respectively.

Their background is:

 

Include search path

 

Usually, when C/C ++ is used for program development, header files are used, and header files are implemented. At this time, three types of files are used: header file source files and header files, implements the source file of the header file. During compilation, the header file and the source file can be used together. Usually they are in the same directory. So there is no problem.

However, when you use some header files of the system, you need to introduce some header files that are not in the current directory. Using an absolute address is a method, but it is a very poor method. Therefore, GCC has a search mechanism. Search for the header file you introduced in the specified folders. This solves the problem. This environment variable is called CPLUS_INCLUDE_PATH. It belongs to GCC and has nothing to do with the system. Let's look at the GCC header file search path.

 

Header file:

1.     # Include "headfile. h"

The search order is:

① Search for the current directory first

② Search for the directory specified by-I

③ Search for the gcc environment variable CPLUS_INCLUDE_PATH (the C program uses C_INCLUDE_PATH)

④ Finally, search for the gcc internal directory

/Usr/include

/Usr/local/include

/Usr/lib/gcc/x86_64-redhat-linux/4.1.1/include

 

When the same file exists in each directory, find which one to use first.

2.     # Include

① Search for the directory specified by-I first

② Search for the gcc environment variable CPLUS_INCLUDE_PATH.

③ Finally, search for the gcc internal directory

/Usr/include

/Usr/local/include

/Usr/lib/gcc/x86_64-redhat-linux/4.1.1/include

 

As shown in the preceding figure, when the directories have the same files, find which one to use first. Note that the # include <> method does not search for the current directory!

 

After searching for the GCC custom environment variable directory, the next internal directory should be the operating system's definition of this header file. This derivation is correct. In fact, this is not the case. The GCC header file search mode follows the "specialized" and "wide" mode. That is to say, most of them use their own set, so they can be found, there may be some common header files. Therefore, the internal directory here is actually not much different from the directory that inherits the operating system.

 

Here we will talk about the internal directory of include. It is not specified by the $ PATH environment variable, but by the prefix of g ++ configuration (it is known that it can be specified when g ++ is installed, I don't know how to modify it after installation. It may be to modify the configuration file. I will study it again when necessary ):

-Bash-3.2 $ g ++-v

Using built-inspecs.

Target: x86_64-redhat-linux

Configured with: ../configure-- Prefix =/usr-- Mandir =/usr/share/man -- infodir =/usr/share/info -- enable-shared -- enable-threads = posix -- enable-checking = release -- with-system-zlib -- enable-_ cxa_atexit -- disable-libunwind-exceptions
-- Enable-libgcj-multifile -- enable-languages ages = c, c ++, objc, obj-c ++, java, fortran, ada -- enable-java-awt = gtk -- disable-dssi -- enable-plugin -- with-java-home =/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre -- with-cpu = generic -- host = x86_64-redhat-linux

Thread model: posix

Gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

 

If the prefix is specified when g ++ is installed, the specified search directory is:

Prefix/include

Prefix/local/include

Prefix/lib/gcc/-- host/-- version/include

During compilation, you can use the-nostdinc ++ option to block the header file for the specified directory search.

 

 

Library search path:

 

After compilation, the program needs to perform Link Operations. As mentioned above, whether the link is dynamic or static, the GCC program must confirm the implementation of the header files "actually exist. Therefore, you need to locate the files. Similar to the include scenario. You can use an absolute directory, but it is not suitable for management. Then the environment variable lib search path appears. LIBRARY_PATH.

Their search path is:

Library file:

During Compilation:

① Gcc will go to-L

② Find the gcc environment variable LIBRARY_PATH.

③ Find the internal directory/lib/usr/local/lib, which was written in the program at the beginning of compile gcc (which cannot be configured ?)

 

Search Path of dynamic library during runtime

(Do not confuse this with the search path of the Library. Here, the program runs in linux/usr/bin/ld to control the process, which is just introduced here. The entire life cycle of the entire program. Compile, Link, start, load (including dynamic loading), execute ):

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

① Specify the dynamic library search path 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)

② Environment variable LD_LIBRARY_PATH specifies the dynamic library search path (when multiple dynamic library search paths are specified through this environment variable, the paths are separated by colons)

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

④ Default dynamic library search path/lib;

⑤ Default dynamic library search path/usr/lib.

 

 

Let's summarize this aspect of the GCC command.

You can use the-I command to load the include search path during compilation.

Use-l for connection-L command can be used to mount the connection search path or file

During execution. Use-Wl to load the search path of the dynamic library when compiling the connection.

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.