《Beginning Linux Programming》读书笔记(一)

xiaoxiao2022-06-12  25

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 ./ hello

2,链接目标文件

<!--<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 . / program

3,打包为静态链接库再链接

<!--<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.a

5,可以使用ldd来查看程序所需要的共享库

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> lddprogram

6,一个简单的脚本

<!--<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
转载请注明原文地址: https://www.6miu.com/read-4933145.html

最新回复(0)