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=.
#optionsfordev
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文件的编写基础篇》
分享到:
相关推荐
Beginning Linux Programming 4th
Beginning Linux Programming, 4th Edition。电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍。
linux程序设计(第三版).pdf 英文版 Beginning Linux Programming 3rd Edition pdf
Beginning Linux Programming.学习linux编程必看的书。非常经典。
Welcome to Beginning Linux Programming, 4th Edition, an easy-to-use guide to developing programs for Linux and other UNIX-style operating systems.
linux编程 Beginning Linux Programming,3rd Ed 2004.pdf
Beginning Linux Programming, Fourth Edition continues its unique approach to teaching UNIX programming in a simple and structured way on the Linux platform. Through the use of detailed and realistic ...
Beginning linux programming(随书源码,非书籍) 从Linux编程开始,第四版继续以独特的方式在Linux平台上以简单和结构化的方式教授UNIX编程。 通过使用详细而实际的示例,学生可以边做边学,并且能够从Linux初学者...
Beginning Linux Programming 英文版第三版
linux程序设计第四版源代码,附加勘误表
Beginning Linux® Programming 4th Edition Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Foreword. . . . . . . . . . . . . . . . . . . . . ....
Beginning Linux Programming, 4th Edition Neil Matthew, Richard Stones 816 pages October 2007
Wrox - Beginning Linux Programming
Beginning Linux Programming 3rd edition.pdf
Beginning Linux Programming, 4th Edition.pdf
Beginning Linux Programming(3th).part01.rarBeginning Linux Programming(3th).part01.rarBeginning Linux Programming(3th).part01.rarBeginning Linux Programming(3th).part01.rar
linux程序设计 英文版 Beginning Linux Programming 4th Edition linux程序设计入门,经典之作,值得一看!