标签:rgb user native 改变 i++ 扩展名 lightdm ref san
之前讲gcc编译的时候,参看:C语言再学习 -- GCC编译过程 提到过静态库和共享库,那时只是简单的讲了下它们相关的编译链接,接下来就该详细介绍它们了。不过再讲解之前还需了解一下编程相关的环境变量。
# echo $HOME /root(2)env:查看所有环境变量
# env LC_PAPER=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8 SSH_AGENT_PID=1768 LC_MONETARY=en_US.UTF-8 GPG_AGENT_INFO=/tmp/keyring-rB9csr/gpg:0:1 TERM=xterm SHELL=/bin/bash XDG_SESSION_COOKIE=619793d1806621208703bfca00000005-1489544306.787815-2133755939 WINDOWID=31457285 LC_NUMERIC=en_US.UTF-8 GNOME_KEYRING_CONTROL=/tmp/keyring-rB9csr USER=root .....
# env | grep SH SSH_AGENT_PID=1768 SHELL=/bin/bash SSH_AUTH_SOCK=/tmp/keyring-rB9csr/ssh SHLVL=1
root@ubuntu:~# set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="2" [2]="24" [3]="1" [4]="release" [5]="i686-pc-linux-gnu") BASH_VERSION=‘4.2.24(1)-release‘ .....(4)export:设置一个新的环境变量 (临时的,重启后消失)
# export HELLO="hello" # echo $HELLO hello(5)unset:清除环境变量
# unset HELLO # echo $HELLO #(6)readonly:设置只读环境变量
# export HELLO="hello" # readonly HELLO="hello" # export HELLO="hello" world bash: HELLO: 只读变量 # unset HELLO bash: unset: HELLO: 无法反设定: 只读 variable
# echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/tarena/workdir/toolchains/opt/S5PV210-crosstools/4.4.6/bin我们可以看到在当前目录下,默认的PATH的值。它表示当我们在当前目录下执行一条命令时命令的搜索路径。每一个目录都是以冒号隔开的。例如:export PATH=$PATH:/opt/arm-2009q1-203/bin:
# echo $HOME /home/admin
# echo $HOME /root(3)HISTSIZE:保存历史命令记录的条数
# echo $HISTSIZE 1000可以看到,上面显示能够保存1000条。
echo $LOGNAME root(5)HOSTNAME:显示主机的名字 (同 指令hostname)
echo $HOSTNAME ubuntu(6)SHELL:指当前用户使用的shell类型
echo $SHELL /bin/bash(7)LANG/LANGUGE:语言相关的环境变量,多语言可以修改此环境变量
echo $LANG zh_CN.UTF-8(8)MAIL:当前用户的邮件存放的目录 (无邮件)
# echo $MAIL(9)PS1:命令基本提示符,对root是 #,对普通用户是 $
# echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$(10)PS2:附属提示符,默认是 >
# echo $PS2 >
gedit /etc/profile 添加 export PATH="/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin:$PATH" 执行 source /etc/profile
gedit .bashrc 添加 export PATH=$PATH:/opt/arm-2009q1-203/bin: 执行 source .bashrc
LIBRARY_PATH The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can‘t find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).2、 man7 上关于LD_LIBRARY_PATH的说明:
LD_LIBRARY_PATH A colon-separated list of directories in which to search for ELF libraries at execution-time. Similar to the PATH environment variable. Ignored in set-user-ID and set-group-ID programs.后面发现 StackOverflow 上关于 LIBRARY_PATH 和 LD_LIBRARY_PATH 的解释更直白:
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program. LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked. EDIT: As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don‘t need to search for the library after your program is compiled and linked. If your library is shared then it needs to be dynamically linked to your program and that‘s when LD_LIBRARY_PATH comes into play.
SEARCH_DIR("/usr/i686-linux-gnu/lib32"); SEARCH_DIR("=/usr/local/lib32"); SEARCH_DIR("=/lib32"); SEARCH_DIR("=/usr/lib32"); SEARCH_DIR("=/usr/local/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib/i386-linux-gnu"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/lib");
//示例一 #include <stdio.h> int main(void) { //声明全局变量 extern char** environ; //给环境表首地址指定替身 char** p = environ; while(*p != NULL) { printf("%s\n",*p); //指向下一个 p++; } return 0; } 功能等同指令env,可自行尝试
//示例二 #include <stdio.h> #include <string.h> int main(void) { //声明全局变量 extern char** environ; //给环境表首地址指定替身 char** p = environ; //练习:查找名字为SHELL的环境变量,获取SHELL的值存到buf的字符数组中,然后进行打印 char buf[20] = {0}; p = environ; while(*p != NULL) { //比较前6个字符是否相等 if(!strncmp(*p,"SHELL=",6)) { //跳过环境变量名= strcpy(buf,*p+6); break; } //比较下一个 p++; } printf("SHELL=%s\n",buf); return 0; } 输出结果: SHELL=/bin/bash 功能等同指令echo $SHELL,可自行尝试
#include <stdio.h> #include <stdlib.h> int main (void) { char *pc = getenv ("SHELL"); if (NULL == pc) perror ("getenv"), exit (-1); printf ("SHELL = %s\n", pc); return 0; } 输出结果: SHELL = /bin/bash 使用echo指令 # echo $SHELL /bin/bash(2)setenv函数
#include <stdio.h> #include <stdlib.h> int main (void) { char *pc = getenv ("SHELL"); if (NULL == pc) perror ("getenv"), exit (-1); printf ("SHELL = %s\n", pc); setenv ("SHELL", "ABC", 1); printf ("修改后的SHELL = %s\n", getenv ("SHELL")); return 0; } 输出结果: SHELL = /bin/bash 修改后的SHELL = ABC(3)putenv函数
#include <stdio.h> #include <stdlib.h> int main (void) { char *pc = getenv ("SHELL"); if (NULL == pc) perror ("getenv"), exit (-1); printf ("SHELL = %s\n", pc); int res = putenv ("SHELL=abc"); if (res) perror("putenv"),exit(-1); printf ("修改后的SHELL = %s\n", getenv ("SHELL")); putenv("CSDN=666"); printf("增加的环境值是:%s\n",getenv("CSDN"));//123 return 0; } 输出结果: SHELL = /bin/bash 修改后的SHELL = abc 增加的环境值是:666(4)unsetenv函数
#include <stdio.h> #include <stdlib.h> int main (void) { putenv("CSDN=666"); printf("增加的环境值是:%s\n",getenv("CSDN"));//123 unsetenv("CSDN"); printf("删除的结果是:%s\n",getenv("CSDN")); return 0; } 输出结果: 增加的环境值是:666 删除的结果是:(null)(5)clearenv函数
#include <stdio.h> #include <stdlib.h> int main (void) { int res; putenv("CSDN=666"); printf("增加的环境值是:%s\n",getenv("CSDN"));//123 res = clearenv (); if(res) perror("clearenv"), exit(-1); printf("清空整个环境表结束\n"); printf("CSDN = %s\n",getenv("CSDN")); return 0; } 输出结果: 增加的环境值是:666 清空整个环境表结束 CSDN = (null)
main函数原型:
int main (int argc, char * argv[], char * envp[]) {....}
第一个参数:命令行参数的个数
第二个参数:命令行参数的地址信息
第三个参数:环境表的首地址
参数argc表示输入参数的个数(含命令名),如果有命令行参数,argc应不小于1;argv表示传入的参数的字符串,是一个字符串数组,argv[0]表示命令名,argv[1]指向第一个命令行参数;至于第三个参数env,它与全局变量environ相比也没有带来更多益处,所以POSIX.1也规定应使用environ而不使用第三个参数。通常用getenv和putenv函数来存取特定的环境变量,而不是直接使用environ变量。例如:
//main函数原型的使用 #include <stdio.h> int main(int argc,char* argv[],char* envp[]) { printf("argc=%d\n",argc); int i=0; for(i=0;i<argc;i++) { printf("感谢%s\n",argv[i]); } //声明全局变量 extern char** environ; printf("environ=%p,envp=%p\n",environ,envp);//直接访问环境表 return 0; } 输出结果为: argc=1 感谢./a.out environ=0xbfab74cc,envp=0xbfab74cc
标签:rgb user native 改变 i++ 扩展名 lightdm ref san
原文地址:http://blog.csdn.net/qq_29350001/article/details/62217915