Table of contents
Basic steps of Linux environment variable configuration
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 printenv
the 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_NAME
printenv
echo $
VARIABLE_NAME
2. Set environment variables
In Linux, export
variables can be set as environment variables using the command. For example, to MY_VAR
set 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 .bashrc
the 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 Esc
the key to exit edit mode, enter to :wq
save 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 .bashrc
the 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_NAME
MY_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 export
commands to set variables as environment variables, and then use them in programs $VARIABLE_NAME
to reference them. For environment variables that need to be automatically loaded on each login, simply add them to .bashrc
the 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.so
shared library named , located /usr/local/lib
under a directory. We can add its path to LD_LIBRARY_PATH
the environment variable with the following command:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
In this way, when we compile libfoo.so
the program we use, the compiler will automatically /usr/local/lib
look for the library file in the directory.
2. Configure the environment variables of the header file
Suppose we have a foo.h
header file named , located /usr/local/include
in a directory. We can add its path to CPLUS_INCLUDE_PATH
the environment variable with the following command:
export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH
In this way, when we compile foo.h
the program we use, the compiler will automatically /usr/local/include
look 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 mylib
Python library called , located /path/to/mylib
under the directory . We can add its path to PYTHONPATH
the 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/mylib
look for the library file in the directory.
In addition, we can also use sys.path
modules 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/mylib
look 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 myprog
executable named , located /path/to/myprog
in a directory. We can add its path to PATH
the 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/to
find 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/bin
or /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.