FAQs related to compilers, program running, and environment variables in Linux

Source: Internet
Author: User

(Updated continuously)


(1) Issues related to environment variables in Linux

(2) GNU toolchain in Linux (GCC/g ++/LD/make and other tools)

(3) The formats of executable files in Linux (mainly elf and A. Out/coff) and the loading of executable files

(4) The search path of the dynamic library loaded by executable program execution

Linux compiling environment variable (LD_LIBRARY_PATH ...)

Http://www.cnwing.net/more.asp? Name = linuxroot & id = 252144

(1) Issues related to environment variables in Linux

1. How to Set environment variables in Linux

A. Enter export on the terminal to set the environment variable
Export Var = Value

Export Var = "value"
Features:Temporary, only valid for the current Shell(That is, if other shell scripts are run in the current command line, the export environment variable defined in the script file is invalid for the "current" shell after the script is executed, because running the script will open a new shell process, of course, this is just a simple prompt ).

1.No space is allowed between VaR and =.. The test is as follows:

$ Export xx = Bash: Export: '=': Invalid identifier $

2. value is optional. If there is no value, the value of VaR is null. (Of course, this is generally not used)
In export Var = value, there are two methods of writing value: quotation marks and no quotation marks. If the value of the value does not contain spaces, the value can be directly written. Otherwise, the VaR value is stopped when it encounters spaces, and the subsequent values are ignored. If the value contains spaces, you can use quotation marks on the value. If you use multiple pairs of quotation marks, it is a link to the string, and quotation marks are not included in the value. The test is as follows:

$export var=abc$echo $varabc$export var= abc$echo $var$export var=abc def$echo $varabc$export var="abc"$echo $varabc$export var= "abc"$echo $var$export var="abc" def$echo $varabc$export var="abc" "def"$echo $varabc$export var="abc def"$echo $varabc def$export var=" abc def g"$echo $varabc def g$export var="abc"def"g"h$echo $varabcdefgh$export var="abc"def"g"h"> xx"$echo $varabcdefgh xx$

Here are just some tests. In fact, these should be shell's requirements for strings. For example, "ABC" def "GH" gets a string like abcdefgh. If the quotation marks are not paired, it is deemed that the input is not completed and the input will continue. What if the value must contain quotation marks? That is, the negative character. As follows:

$export var=abc\"def$echo $varabc"def$export var=abc\"def\"$echo $varabc"def"$export var="abc\"def\"""gh"$echo $varabc"def"gh$

In short, it is easy to understand these tests, and they are generally not used. Mainly knowThe function of quotation marks is OK, and it is not included in the value of var..

B. Add environment variables in/etc/profile
Is to open the file, add the export content in it, the use of export is the same as above.
Features:Permanent and valid for all users.
Note: If you modify the file under the command line, the setting will not take effect immediately after exiting the editing. You need to run source/etc/profile. If you do not run source, this will take effect the next time you start logging on to this user. Refer to the usage of the Linux source command.

C... bash_profile (~ /. Bash_profile) to add Environment Variables

The method is the same as that added in/etc/profile,

Features:Permanent, but only valid for the current user.

Note: It corresponds to/etc/profile, so it takes effect immediately after you set it. Otherwise, you need to log on to the user again.

D. Other Methods

The above three methods are common methods for setting environment variables. For the BC method, if you do not use the source command, You need to log on to the user again to take effect, there are also some other Bash configuration files that can be set. The main point here is under the user directory. bashrc file (~ /. Bashrc), which corresponds to the. bash_profile setting. The difference is that. bashrc takes effect every time you restart the shell (command line) and does not need to log on to the user again. Of course, you may also set environment variables for other files, but the principles and methods are the same. Refer to the relevant content to compare the differences between these configuration files and understand their principles.

Note: You are used to export and ~ /. Bashrc method, which should also be a common method of use.

2. How to reference (represent) Another environment variable

After defining an environment variable, use$ VaRThe environment variable is referenced.

2. How to display environment variables (values) in Linux)

A. use echo to display a single environment variable

As mentioned above, directly ECHO can output the value of a single environment variable. In fact, this is just an example of referencing the environment variable. You can reference the environment variable with $ var, shell will translate it into its vaue, so naturally, the following is also possible:

$export var=test$echo $var"add something"testadd something$echo $var add somethingtest add something$

B. Use env to display all environment variables and their values

It is equivalent to using echo $ var for all environment variables. You can directly input the Env command through the command line.

C. Use set to display all user Variables

The SET command displays user variables. For more information about environment variables and user variables, see. (Note: You can directly define user variables, such as Var = value. The reference to user variables is also $ var. For more information, see shell programming) (user environment variables are also part of user variables ).

Note: unset can delete user variables, such as unset var. Note: It is not unset $ var! Unset can delete user variables and functions. environment variables are only a small part of them. Of course, the so-called "delete" variable actually sets it to null, and the effect is the same (how to set it? Direct assignment! For example, $ Var = newvalue is also applicable to environment variables and common user variables.

(2) problems related to GNU tool chain in Linux

1. GNU tool chain introduction (including content)

Wiki: http://zh.wikipedia.org/wiki/GNU_toolchain

The GNU toolchain is a collection of various programming tools generated by the GNU project. These tools form a tool chain (a set of tools used in serial mode) for developing applications and operating systems. The GNU tool chain contains the following items:
GNU make: an automatic tool for compiling and building;
GNU Compiler set (GCC): a set of compilers in multiple programming languages;
GNU binutils: A tool set that contains the linker, assembler, and other tools;
GNU Debugger (GDB): A Code debugging tool;
GNU build system (autotools ):

Because there are many tools, I will not describe them one by one. Note: GNU toolchain does not only refer to GCC, G ++, and lD.

2. Program compilation in Linux (similar to Windows)

Reference: http://blog.csdn.net/gengshenghong/article/details/7096709

3. the formats of executable files (mainly elf and A. Out/coff) and the loading of executable files in Linux

The format of executable files in Linux. Currently, the executable programs on Linux are basicallyElfThe. So shared Dynamic Link Library is also in the ELF format.

You can use the file command to view the file format on Linux. The following is the result of using file for two test files:

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, not stripped

This is a result of using File A. out after compiling Hello world. The following is the result of using file for a dynamic link library:

libBox2D.so.2.1.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped

For more information about the formats of executable files, see the following:



Http://www.linuxsir.org/bbs/printthread.php? T = 206356



At least, we need to know that there is a loading process before running the program on Linux, including loading dynamic link library (Dynamic Link Dynamic Linking ), that is, load the dynamic link library, refer to the http://www.linuxsir.org/bbs/printthread.php? T = 206356. In addition,In Linux, the loader of the dynamic link library is completed by LD. So.(Http://zh.wikipedia.org/wiki/Ld.so), note, not the linker lD! For more information about lD. So, see http://www.cnblogs.com/cornsea/archive/2009/12/09/1620470.html. As follows:

##/lib/ld-linux.so.2 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]You have invoked `ld.so', the helper program for shared library executables.This program usually lives in the file `/lib/ld.so', and special directivesin executable files using ELF shared libraries tell the system's programloader to load the helper program from this file.  This helper program loadsthe shared libraries needed by the program executable, prepares the programto run, and runs it.  You may invoke this helper program directly from thecommand line to load and run an ELF executable file; this is like executingthat file itself, but always uses this helper program from the file youspecified, instead of the helper program file specified in the executablefile you run.  This is mostly of use for maintainers to test new versionsof this helper program; chances are you did not intend to run this program.  --list                list all dependencies and how they are resolved  --verify              verify that given object really is a dynamically linkedobject we can handle  --library-path PATH   use given PATH instead of content of the environmentvariable LD_LIBRARY_PATH  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object namesin LIST  --audit LIST          use objects named in LIST as auditors##

Related commands:

File XXX: view the file type of file xxx

LDD xxx: View the dynamic link library on which the executable file XXX depends.

NM: list the symbols in the file (symbols ).

4. The search path of the dynamic library loaded by executable program execution

Reference: http://blog.csdn.net/benben8901/article/details/6714160


Note: LD_LIBRARY_PATH, which is used by LD. So, so it has nothing to do with the connection phase of the compiler.

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.