《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);
}
复制代码

再来看第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);
}
复制代码

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;
}
复制代码

hello.h

void sayHello();

hello.c

#include "hello.h"

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
复制代码

执行

make 
.
/helloworld
make clean

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

复制代码
OBJFILE = main.o hello.o
CC 
= gcc
CFLAGS 
= -Wall --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 --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 --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

 

参考资料:

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

posted on   Phinecos(洞庭散人)  阅读(778)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 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的设计模式综述

导航

统计

点击右上角即可分享
微信分享提示