我的学习之旅(8) Makefile

49 篇文章 0 订阅
49 篇文章 0 订阅

build_setup.c源代码

#include <stdio.h> 
#include <string.h>
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h>
#include <linux/fs.h>
#include <unistd.h> 
#include <fcntl.h>

#define MINIX_HEADER_LEN 32

int main(int argc, char *argv[] )
{
 char buf[2048];
 int fd;
 int len;

 if( argc != 3 ) {
  printf("no input file\n");
  return 0;
 }
 sprintf(buf,"rm -f %s", argv[2]);
 system(buf);
 sprintf(buf,"touch %s", argv[2]);
 system(buf);

 fd = open(argv[1], O_RDONLY, 0 );
 if( fd < 0 ){
  printf("open file:%s failed\n", argv[1]);
  return 0;
 }
 if( read(fd, buf, MINIX_HEADER_LEN)!= MINIX_HEADER_LEN) {
  close(fd);
  printf("Invalid mini header len:%d\n", MINIX_HEADER_LEN);
  return 0;
 }
 memset(buf, 0x0, sizeof(buf));
 len = read(fd, buf, sizeof(buf));
 /* printf("%02x %02x %02x %02x\n",buf[0],buf[1],buf[2],buf[3]);*/
 close(fd);
        fd = open(argv[2], O_WRONLY, O_CREAT | O_EXCL | O_SYNC );
        if( fd < 0 ){
                printf("open file:%s failed\n", argv[2]);
                return 0;
        }

 len = write(fd, buf, len);

 close(fd);
 return 0;
}

写成Makefile:

boot:   boot.s setup.s build_boot.c build_setup.c
        as86 -0 -a -o boot.o boot.s
        ld86 -0 -s -o boot boot.o

        as86 -0 -a -o setup.o setup.s
        ld86 -0 -s -o setup setup.o

        gcc  -Wall -O2 -fomit-frame-pointer -o build_boot build_boot.c 
        gcc -Wall -O2 -fomit-frame-pointer -o build_setup build_setup.c

        ./build_boot boot bootsec
        ./build_setup setup setupsec
        rm -f Image
        cat bootsec setupsec >./boot_partition
all:
        boot

clean:
        rm -rf *.o
        rm -rf boot setup build_boot build_setup bootsec setupsec boot_partition build_image
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值