Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2115401
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-09-01 17:21:01

1.1 结构目录
  1. cong@msi:/work/os/code/7int$ tree
  2. .
  3. ├── boot
  4. │   ├── loader.S
  5. │   └── mbr.S
  6. ├── include
  7. │   ├── interrupt.h
  8. │   ├── io.h
  9. │   ├── print.h
  10. │   ├── stdarg.h
  11. │   └── stdint.h
  12. ├── init
  13. │   └── main.c
  14. ├── kernel
  15. │   ├── interrupt.c
  16. │   ├── kernel.S
  17. │   └── Makefile        -->每个目录下的Makefile,都编译成一个静态库
  18. ├── lib
  19. │   ├── Makefile        -->每个目录下的Makefile,都编译成一个静态库
  20. │   ├── printf.c
  21. │   └── put_char.S
  22. └── Makefile           -->总的Makefile,链接所有的静态库与main.o
1.2 顶层的Makefile
  1. cong@msi:/work/os/code/7int$ cat Makefile
  2. ENTRY_POINT = 0xc0001500
  3. AS = nasm
  4. CC = gcc
  5. CPP =gcc -E -nostdinc -I ./include
  6. LD = ld
  7. INCLUDE = -I ./include
  8. ASFLAGS = -f elf
  9. CFLAGS = -Wall $(INCLUDE) -c -m32 -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes -nostdinc
  10. LDFLAGS = -Ttext $(ENTRY_POINT) -m elf_i386 -e main -Map System.map
  11. LIBS = lib/lib.a
  12. KERNEL= kernel/kernel.a

  13. all: mbr.bin loader.bin init/kernel.bin
  14. mbr.bin:
  15.     $(AS) ./boot/mbr.S -I ./boot/include/ -o ./boot/mbr.bin
  16. loader.bin:
  17.     $(AS) ./boot/loader.S -I ./boot/include/ -o ./boot/loader.bin
  18. init/kernel.bin: init/main.o $(KERNEL) $(LIBS)
  19.     $(LD) $(LDFLAGS) -o $@ $^         -->把各个目录下的库都链接在一起

  20. init/main.o: init/main.c
  21.     $(CC) $(CFLAGS) -c -o $*.o $<

  22. $(KERNEL):
  23.     (cd kernel; make)
  24. $(LIBS):
  25.      (cd lib; make)

  26. clean:
  27.     -rm /work/os/code/disk.img ./boot/mbr.bin ./boot/loader.bin init/kernel.bin
  28.     -rm -f Image System.map tmp_make core
  29.     -rm -f init/*.o boot/*.o
  30.     (cd kernel;make clean)
  31.     (cd lib;make clean)

  32. flash:
  33.     -rm /work/os/code/disk.img
  34.     dd if=/dev/zero of=/work/os/code/disk.img bs=1M count=30
  35.     dd if=./boot/mbr.bin of=/work/os/code/disk.img bs=512 count=1 conv=notrunc
  36.     dd if=./boot/loader.bin of=/work/os/code/disk.img bs=512 count=5 seek=2 conv=notrunc
  37.     dd if=./init/kernel.bin of=/work/os/code/disk.img bs=512 count=200 seek=9 conv=notrunc
  38. show:
  39.     echo "show loader.bin"
  40.     xxd ./loader.bin
  41.     echo "show disk.img"
  42.     xxd -seek 0x400 -l 0x200 /work/os/code/disk.img
  43. dep:                     -->这个dep是从linux0.11里面抄来的
  44.     sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
  45.     (for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make
  46.     cp tmp_make Makefile
  47.     (cd kernel; make dep)
  48.     (cd lib; make dep)
1.2.a 说明
规则里面有三部分: mbr.bin loader.bin init/kernel.bin 
其中mbr.bin loader.bin 以后就不用再动了
只需要init/kernel.bin需要改动



1.3 每个目录下的Makefile
  1. cong@msi:/work/os/code/7int$ cat lib/Makefile
  2. AS = nasm
  3. CC = gcc
  4. CPP =gcc -E -nostdinc -I ../include
  5. LD = ld
  6. INCLUDE= -I ../include
  7. ASFLAGS = -f elf
  8. CFLAGS = -Wall $(INCLUDE) -c -m32 -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes -nostdinc
  9. LDFLAGS = -m elf_i386

  10. TARGET = lib.a
  11. OBJS= put_char.o printf.o      -->以后直接在这儿添加就行了

  12. .c.o:
  13.     $(CC) $(CFLAGS) -c -o $*.o $<
  14. .S.o:
  15.     $(AS) $(ASFLAGS) -o $*.o $<

  16. all: $(TARGET)
  17. $(TARGET):$(OBJS)
  18.     $(AR) rcs $(TARGET) $(OBJS)          -->把该目录下的所有目标文件都打包成库,然后在顶层的Makefile中链接成kernel.bin
  19. clean:
  20.     rm -f core *.o *.a tmp_make
  21.     for i in *.c;do rm -f `basename $$i .c`.s;done

  22. dep:             -->这个dep是从linux0.11里面抄来的
  23.     sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
  24.     (for i in *.c;do $(CPP) -M $$i;done) >> tmp_make
  25.     cp tmp_make Makefile
1.3.a 说明 
以后新添加文件进行编译时,如要在lib下添加test.c, 只需要OBJS里面加入test.o就可以了,其它的都不用动
1.4 代码打包
7int_Makefile.rar (下载后改名为7int_Makefile.tar.gz)
阅读(890) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~