`
phinecos
  • 浏览: 342928 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

 
阅读更多

1,读写空指针

先看第一种情况,printf试图访问空指针,以打印出字符串,而sprintf试图向空指针写入字符串,这时,linux会在GNU C函数库的帮助下,允许读空指针,但不允许写空指针。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<unistd.h>
#include
<stdlib.h>
#include
<stdio.h>

intmain()
{
char*some_memory=(char*)0;
printf(
"Areadfromnull%s/n",some_memory);
sprintf(some_memory,
"Awritetonull/n");
exit(EXIT_SUCCESS);
}

再来看第2种情况,直接读地址0的数据,这时没有GNU libc函数库在我们和内核之间了,因此不允许执行。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<unistd.h>
#include
<stdlib.h>
#include
<stdio.h>

intmain()
{
charz=*(constchar*)0;
printf(
"Ireadfromlocationzero/n");
exit(EXIT_SUCCESS);
}

2,今天看到400页了,做个小结。第三章文件和目录,第四章参数,选项处理和环境变量,第五章和第六章都是终端的编程,第7章内存管理和文件加锁机制,包括整个文件封锁和部分区域封锁,第八章MySQL跳过不看,

3,使用make命令就可以完成编译,修改某些源码后再次make就可以编译修改部分与其他引用到的文件。下面是最简单的一个示例:

main.c

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<stdio.h>
#include
"hello.h"

intmain()
{
printf(
"hi,firstline/n");
sayHello();
return0;
}

hello.h

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

hello.c

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include"hello.h"

voidsayHello()
{
printf(
"hello,world/n");
}

Makefile

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->helloworld:main.ohello.o
gcc
-ohelloworldmain.ohello.o
main.o:main.chello.h
gcc
-cmain.c-omain.o
hello.o:hello.chello.h
gcc
-chello.c-ohello.o
clean:
rm
-rf*.ohelloworld

执行

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

4,对上面Makefile的第一个改进版本

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->OBJFILE=main.ohello.o
CC
=gcc
CFLAGS
=-Wall-O-g

helloworld:$(OBJFILE)
$(CC)
-ohelloworld$(OBJFILE)
main.o:main.chello.h
$(CC)$(CFLAGS)
-cmain.c-omain.o
hello.o:hello.chello.h
$(CC)$(CFLAGS)
-chello.c-ohello.o
clean:
rm
-rf*.ohelloworld

5,对上面Makefile的第二个改进版本

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CC=gcc
CFLAGS
=-Wall-O-g
TARGET
=./helloworld

%.o:%.c
$(CC)$(CFLAGS)
-c$<-o$@
SOURCES
=$(wildcard*.c)
OBJFILE
=$(patsubst%.c,%.o,$(SOURCES))
$(TARGET):$(OBJFILE)
$(CC)$(OBJFILE)
-o$(TARGET)
chmoda
+x$(TARGET)
clean:
rm
-rf*.o$(TARGET)

6,上面Makefile的第三个改进版本,加入install功能

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#whichcomplier
CC
=gcc
#
wheretoinstall
INSTDIR
=/usr/local/bin
#
whereareincludefileskept
INCLUDE
=.
#options
fordev
CFLAGS
=-Wall-O-g

TARGET
=./helloworld

%.o:%.c
$(CC)$(CFLAGS)
-c$<-o$@
SOURCES
=$(wildcard*.c)
OBJFILE
=$(patsubst%.c,%.o,$(SOURCES))
$(TARGET):$(OBJFILE)
$(CC)$(OBJFILE)
-o$(TARGET)
chmoda
+x$(TARGET)
clean:
rm
-rf*.o
install:$(TARGET)
@if[
-d$(INSTDIR)];/
then/
cp$(TARGET)$(INSTDIR);/
chmoda
+x$(INSTDIR)/$(TARGET);/
chmodog
-w$(INSTDIR)/$(TARGET);/
echo
"installedin$(INSTDIR)";/
else/
echo
"sorry,$(INSTDIR)doesnotexist";/
fi

执行:

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

参考资料:

1,《Linux平台Makefile文件的编写基础篇

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics