1,第一个程序
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < stdio.h > int main() { printf( " hello,linux/n " ); return 0 ; }编译运行:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> gcc-ohellohello . c ./ hello2,链接目标文件
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < stdio.h > void fred( int arg) { prinft( " fred:you%/n " ,arg); } <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < stdio.h > void bill( char * arg) { printf( " bill:youpassed%s/n " ,arg); }编译:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> gcc–cbill.cfred.c <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /* Thisislib.h.Itdeclaresthefunctionsfredandbillforusers */ void bill( char * ); void fred( int ); <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include " lib.h " int main() { bill( " HelloWorld " ); fred( 100 ); exit( 0 ); }编译运行:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> gcc–cprogram.c gcc–oprogramprogram.obill.ofred.o . / program3,打包为静态链接库再链接
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> arcrvlibfoo.abill.ofred.o gcc - oprogramprogram.olibfoo.a . / program也可以这样
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> gcc - oprogramprogram.o - L. - lfoo这里的-L.就指明编译器在当前目录下寻找库文件,-lfoo告诉编译器使用名为libfoo.a的静态库(或名为libfoo.so的共享库)
4,要查看obj文件,库或可执行文件中包含的函数,可以使用nm命令,
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> nmlibfoo.a5,可以使用ldd来查看程序所需要的共享库
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> lddprogram6,一个简单的脚本
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> # !/ bin / bash for file in * do if grep - qbash$file then echo$file fi done exit 0可以有两种执行方式
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> / bin / bashfirst或者先改变脚本文件的权限,给用户加上可执行权限
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> chmodu + xfirst然后直接执行
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> first如果报错说“找不到命令“,则说明Shell环境变量PATH中没有设置当前目录这一项,可以有两种方式改变,要么输入”PATH=$PATH:. “,再用”export”使之生效,要么编辑.bash_profile文件,将这个命令加入到文件末尾,然后登出再登陆回来。当然另一种暂行的方法是输入”./first”。当然最后一种方式是linux推荐的。
另外,我们可以将上面这个脚本文件放到别的目录下共享
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> sudocpfirst / usr / local / bin chmod 755 / usr / local / bin / first最后一行是给予组用户和其他用户执行权限
7,在Windows下,查看环境变量的命令是:set,这个命令会输出系统当前的环境变量,那Linux下应该如何查看呢,命令是:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> export如果你想查看某一个名称的环境变量,命令是:echo $环境变量名,比如:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> echo$JAVAHOME--------------------------------------------------------------------------------
设置环境变量
如果使用的是bash外壳,则键入如下命令:
JAVA_HOME=/ path/ to/ jdk
export JAVA_HOME
其中/path/to/jdk是安装Java的路径。
如果使用的是tcsh,则键入如下命令:
setenv JAVA_HOME /path/to/jdk
--------------------------------------------------------------------------------
删除环境变量
字符模式下设置/删除环境变量
bash下
设置:export 变量名=变量值
删除:unset 变量名
csh下
设置:setenv 变量名 变量值
删除:unsetenv 变量名
相关资源:Linux高级编程_Advanced_Linux_Programming.pdf