《Beginning Linux Programming》读书笔记(四)
1, 读写空指针
先看第一种情况,printf试图访问空指针,以打印出字符串,而sprintf试图向空指针写入字符串,这时,linux会在GNU C函数库的帮助下,允许读空指针,但不允许写空指针。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *some_memory = (char *)0;
printf("A read from null %s\n", some_memory);
sprintf(some_memory, "A write to null\n");
exit(EXIT_SUCCESS);
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *some_memory = (char *)0;
printf("A read from null %s\n", some_memory);
sprintf(some_memory, "A write to null\n");
exit(EXIT_SUCCESS);
}
再来看第2种情况,直接读地址0的数据,这时没有GNU libc函数库在我们和内核之间了,因此不允许执行。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char z = *(const char *)0;
printf("I read from location zero\n");
exit(EXIT_SUCCESS);
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
char z = *(const char *)0;
printf("I read from location zero\n");
exit(EXIT_SUCCESS);
}
2,今天看到400页了,做个小结。第三章文件和目录,第四章参数,选项处理和环境变量,第五章和第六章都是终端的编程,第7章内存管理和文件加锁机制,包括整个文件封锁和部分区域封锁,第八章MySQL跳过不看,
3,使用make命令就可以完成编译,修改某些源码后再次make就可以编译修改部分与其他引用到的文件。下面是最简单的一个示例:
main.c
#include <stdio.h>
#include "hello.h"
int main()
{
printf("hi,first line\n");
sayHello();
return 0;
}
#include "hello.h"
int main()
{
printf("hi,first line\n");
sayHello();
return 0;
}
hello.h
void sayHello();
hello.c
#include "hello.h"
void sayHello()
{
printf("hello,world\n");
}
void sayHello()
{
printf("hello,world\n");
}
Makefile
helloworld: main.o hello.o
gcc -o helloworld main.o hello.o
main.o: main.c hello.h
gcc -c main.c -o main.o
hello.o: hello.c hello.h
gcc -c hello.c -o hello.o
clean:
rm -rf *.o helloworld
gcc -o helloworld main.o hello.o
main.o: main.c hello.h
gcc -c main.c -o main.o
hello.o: hello.c hello.h
gcc -c hello.c -o hello.o
clean:
rm -rf *.o helloworld
执行
make
./helloworld
make clean
./helloworld
make clean
4,对上面Makefile的第一个改进版本
OBJFILE = main.o hello.o
CC = gcc
CFLAGS = -Wall -O -g
helloworld: $(OBJFILE)
$(CC) -o helloworld $(OBJFILE)
main.o: main.c hello.h
$(CC) $(CFLAGS) -c main.c -o main.o
hello.o: hello.c hello.h
$(CC) $(CFLAGS) -c hello.c -o hello.o
clean:
rm -rf *.o helloworld
CC = gcc
CFLAGS = -Wall -O -g
helloworld: $(OBJFILE)
$(CC) -o helloworld $(OBJFILE)
main.o: main.c hello.h
$(CC) $(CFLAGS) -c main.c -o main.o
hello.o: hello.c hello.h
$(CC) $(CFLAGS) -c hello.c -o hello.o
clean:
rm -rf *.o helloworld
5,对上面Makefile的第二个改进版本
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)
chmod a+x $(TARGET)
clean:
rm -rf *.o $(TARGET)
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)
chmod a+x $(TARGET)
clean:
rm -rf *.o $(TARGET)
6,上面Makefile的第三个改进版本,加入install功能
#which complier
CC = gcc
#where to install
INSTDIR = /usr/local/bin
#where are include files kept
INCLUDE = .
#options for dev
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)
chmod a+x $(TARGET)
clean:
rm -rf *.o
install: $(TARGET)
@if [ -d $(INSTDIR) ]; \
then \
cp $(TARGET) $(INSTDIR);\
chmod a+x $(INSTDIR)/$(TARGET);\
chmod og-w $(INSTDIR)/$(TARGET);\
echo "installed in $(INSTDIR)";\
else \
echo "sorry,$(INSTDIR) does not exist";\
fi
CC = gcc
#where to install
INSTDIR = /usr/local/bin
#where are include files kept
INCLUDE = .
#options for dev
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)
chmod a+x $(TARGET)
clean:
rm -rf *.o
install: $(TARGET)
@if [ -d $(INSTDIR) ]; \
then \
cp $(TARGET) $(INSTDIR);\
chmod a+x $(INSTDIR)/$(TARGET);\
chmod og-w $(INSTDIR)/$(TARGET);\
echo "installed in $(INSTDIR)";\
else \
echo "sorry,$(INSTDIR) does not exist";\
fi
执行:
make install
make clean
make clean
参考资料:
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
分类:
linux
posted on 2008-11-25 18:38 Phinecos(洞庭散人) 阅读(778) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述