Explain in detail the configuration of environment variables under Linux, C++ library files and header files, and python library

Table of contents

Basic steps of Linux environment variable configuration

1. View environment variables

2. Set environment variables

3. Permanently set environment variables

4. Using environment variables

C++ library file and header file environment variable configuration

1. Configure the environment variables of the so library file

2. Configure the environment variables of the header file

Python library environment variable configuration

Linux Configuration Execution File Environment Variables


We are all used to configuring environment variables on Windows. For example, to set system environment variables on Windows, just add the configuration path to the path; use VS to configure the library file and header file path in the project settings; use vscode to compile with cmake and write CMakeLists .txt Link library files or header files are fine.

In the Linux environment, you need to remember some common commands and constant values, which are summarized as follows for reference.

Basic steps of Linux environment variable configuration

In Linux, environment variables are some global variables that can be accessed by programs and can be used to configure system behavior, define runtime parameters of software, and other purposes. The following are the detailed steps of environment variable configuration:

1. View environment variables

Use printenvthe or command to view the current environment variables. The command can list all environment variables, and the command can output the value of the specified environment variable.echo $VARIABLE_NAMEprintenvecho $VARIABLE_NAME

2. Set environment variables

In Linux, exportvariables can be set as environment variables using the command. For example, to MY_VARset to hello, you can run

export MY_VAR=hello

It is also possible to set multiple variables as environment variables, for example:

export MY_VAR1=value1 export MY_VAR2=value2

3. Permanently set environment variables

If you want to automatically load environment variables every time you log in, you need to add the environment variables to .bashrcthe file. This file is each user's personal bash profile and is automatically loaded when the user logs in. This file can be opened with the following command:

vi ~/.bashrc

Add the following at the end of the file:

export MY_VAR=value

Then press Escthe key to exit edit mode, enter to :wqsave and exit.

 After the configuration is complete, you need to execute

source ~/.bashrc 

command to make the environment variable take effect. This command will reload .bashrcthe file to make the configured environment variables take effect.

4. Using environment variables

In a program, you can use to refer to the value of an environment variable. For example, in a bash script, the following code can be used to get the value of :$VARIABLE_NAMEMY_VAR

echo $MY_VAR

This will output hello, which is the value we set in step 2.

In short, the configuration of environment variables in Linux is very simple. You only need to use exportcommands to set variables as environment variables, and then use them in programs $VARIABLE_NAMEto reference them. For environment variables that need to be automatically loaded on each login, simply add them to .bashrcthe file.

C++ library file and header file environment variable configuration

When compiling a C++ program, if we depend on some external library files or header files, we need to tell the compiler where they are. A common practice is to set these paths as environment variables so that the compiler can automatically find them during compilation. Here are two cases:

1. Configure the environment variables of the so library file

Suppose we have a libfoo.soshared library named , located /usr/local/libunder a directory. We can add its path to LD_LIBRARY_PATHthe environment variable with the following command:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

In this way, when we compile libfoo.sothe program we use, the compiler will automatically /usr/local/liblook for the library file in the directory.

2. Configure the environment variables of the header file

Suppose we have a foo.hheader file named , located /usr/local/includein a directory. We can add its path to CPLUS_INCLUDE_PATHthe environment variable with the following command:

export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH

In this way, when we compile foo.hthe program we use, the compiler will automatically /usr/local/includelook for the header file in the directory.

Of course, we can also write these two instructions into ~/.bashrc to make them take effect permanently.

In short, it is a common practice to configure the environment variables of C++ so library files or header files, which allows the compiler to automatically find them during compilation and improve compilation efficiency.

Python library environment variable configuration

In Python, we can also tell the Python interpreter where to find library files by setting environment variables. Here is an example:

Suppose we have a mylibPython library called , located /path/to/mylibunder the directory . We can add its path to PYTHONPATHthe environment variable with the following command:

export PYTHONPATH=/path/to/mylib:$PYTHONPATH

In this way, when we use it in Python import mylib, the Python interpreter will automatically /path/to/myliblook for the library file in the directory.

In addition, we can also use sys.pathmodules to dynamically add the path of the Python library, for example:

import sys
sys.path.append('/path/to/mylib')
import mylib

In this way, the Python interpreter will also automatically /path/to/myliblook for the library file in the directory.

Linux Configuration Execution File Environment Variables

Configuring the environment variable of the executable file allows the executable file to be executed directly in any path without running under the path of the executable file. Here is an example:

Suppose we have an myprogexecutable named , located /path/to/myprogin a directory. We can add its path to PATHthe environment variable so that the executable can be run directly under any path. For example:

export PATH=/path/to:$PATH

In this way, when we execute in any path myprog, the system will automatically /path/tofind the executable file in the directory and execute it.

In addition, we can also copy the executable file to the standard path of the system (such as /usr/binor /usr/local/bin), so that the executable file can be run directly in any path. But this practice may require administrator privileges.

It should be noted that too many environment variable settings may affect system performance and security, so they should be used with caution.

Guess you like

Origin blog.csdn.net/weixin_40582034/article/details/129311846