c高级-Makefile

一、makefile的制作

(1)完整的程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
	char name[20];
	int age;
	int score[3];
	int average;
}STU;

STU *  MALLOC(int n,STU** st)
{
	*st = (STU *)malloc(sizeof(STU)*n);
	if(NULL == *st)
	{
		return NULL;
	}
	memset(*st,0,sizeof(STU)*n);
	return *st;
}

int INPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%s%*c%d",(st+i)->name,&((st+i)->age));
		for(j=0;j<3;j++)
		{
			scanf("%d",&((st+i)->score[j]));
		}
	}
}

int OUTPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		printf("%s  %d",(st+i)->name,((st+i)->age));
		for(j=0;j<3;j++)
		{
			printf("%d ",((st+i)->score[j]));
		}
	}
		printf("\n");
}

int AVERAGE(int n,STU*st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<3;j++)
		{
			(st+i)->average += ((st+i)->score[j]);
		}
		(st+i)->average = (st+i)->average/3;
		printf("%d\n",(st+i)->average);
		if((st+i)->average > 85)
		{
			printf("%s",(st+i)->name);
		}
	}
}

int MAXADDRESS(int n,STU * st)
{
	int i = 0;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(s->age < (st+i)->age)
		{
			s = st+i;
		}
	}
		printf("%p\n",s);
}

int MAXNAME(int n,STU * st)
{
	int i;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(strcmp(s->name,(st+i)->name)>0)
		{

		}else
		{
			s = st+i;
		}
	}
	printf("%s",s->name);
}

int main(int argc, const char *argv[])
{
	STU s;
	STU * stu = &s;
	MALLOC(3,&stu);
	printf("-----------\n");
	INPUT(3,stu);
	printf("-----------\n");
	OUTPUT(3,stu);
	printf("-----------\n");
	AVERAGE(3,stu);
	printf("-----------\n");
	MAXADDRESS(3,stu);
	printf("-----------\n");
	MAXNAME(3,stu);
	return 0;
}

接下来将其拆分成.h的头文件,.c的程序文件和makefile脚本

(1.1)头文件head.h
#ifndef _HEAD_H_
#define _HEAD_H_ 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
	char name[20];
	int age;
	int score[3];
	int average;
}STU;

STU *  MALLOC(int n,STU** st);
int INPUT(int n,STU * st);
int OUTPUT(int n,STU * st);
int AVERAGE(int n,STU*st);
int MAXADDRESS(int n,STU * st);
int MAXNAME(int n,STU * st);

#endif
(1.2) MALLOC.c
  #include"head.h"
STU *  MALLOC(int n,STU** st)
{
	*st = (STU *)malloc(sizeof(STU)*n);
	if(NULL == *st)
	{
		return NULL;
	}
	memset(*st,0,sizeof(STU)*n);
	return *st;
}
(1.3) INPUT.c
#include"head.h"
int INPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%s%*c%d",(st+i)->name,&((st+i)->age));
		for(j=0;j<3;j++)
		{
			scanf("%d",&((st+i)->score[j]));
		}
	}
}
(1.4) OUTPUT.c
#include"head.h"
int OUTPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		printf("%s  %d",(st+i)->name,((st+i)->age));
		for(j=0;j<3;j++)
		{
			printf("%d ",((st+i)->score[j]));
		}
	}
		printf("\n");
}
(1.5) AVERAGE.c
#include"head.h"
int AVERAGE(int n,STU*st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<3;j++)
		{
			(st+i)->average += ((st+i)->score[j]);
		}
		(st+i)->average = (st+i)->average/3;
		printf("%d\n",(st+i)->average);
		if((st+i)->average > 85)
		{
			printf("%s",(st+i)->name);
		}
	}
}
(1.6) MAXADDRESS.c
#include"head.h"
int MAXADDRESS(int n,STU * st)
{
	int i = 0;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(s->age < (st+i)->age)
		{
			s = st+i;
		}
	}
		printf("%p\n",s);
}
(1.7) MAXNAME.c
#include"head.h"
int MAXNAME(int n,STU * st)
{
	int i;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(strcmp(s->name,(st+i)->name)>0)
		{

		}else
		{
			s = st+i;
		}
	}
	printf("%s",s->name);
}
(1.8) main.c
  #include"head.h"
int main(int argc, const char *argv[])
{
	STU s;
	STU * stu = &s;
	MALLOC(3,&stu);
	printf("-----------\n");
	INPUT(3,stu);
	printf("-----------\n");
	OUTPUT(3,stu);
	printf("-----------\n");
	AVERAGE(3,stu);
	printf("-----------\n");
	MAXADDRESS(3,stu);
	printf("-----------\n");
	MAXNAME(3,stu);
	return 0;
}
(1.9)makefile脚本
CC=gcc    #将gcc当作系统预定义的编译器
CFLAGS=-c #将-c当作系统预定义的编译器
<<=-o     #用<<替换-o
export CC CFLAGS


APP:MALLOC.o INPUT.o OUTPUT.o AVERAGE.o MAXADDRESS.o MAXNAME.o MAIN.o
	${CC}  $^ -o $@                              # gcc *.c -o *.o
#MALLOC.o:MALLOC.c
#	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c MALLOC.c -o MALLOC.o
INPUT.o:INPUT.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c INPUT.c -o INPUT.o 
OUTPUT.o:OUTPUT.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c OUTPUT.c -o OUTPUT.o 
AVERAGE.o:AVERAGE.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c AVERAGE.c -o AVERAGE.o 
MAXADDRESS.o:MAXADDRESS.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c MAXADDRESS.c -o MAXADDRESS.o 
MAXNAME.o:MAXNAME.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c MAXNAME.c -o MAXNAME.o 
MAIN.o:MAIN.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c MAIN.c -o MAIN.o 
MALLOC.o:MALLOC.c
	${CC} ${CFLAGS} $< ${<<} $@                  # gcc -c MALLOC.c -o MALLOC.o 
.PHONY:
h:
	
	rm *.o
	rm APP
(2)执行

运行:make #运行所有代码

运行:make h #执行 rm *.o rm APP

(3)结果
linux@ubuntu:~/makefile$ make
gcc     -c  MALLOC.c -o      MALLOC.o 
gcc     -c  INPUT.c -o      INPUT.o 
gcc     -c  OUTPUT.c -o      OUTPUT.o 
gcc     -c  AVERAGE.c -o      AVERAGE.o 
gcc     -c  MAXADDRESS.c -o      MAXADDRESS.o 
gcc     -c  MAXNAME.c -o      MAXNAME.o 
gcc     -c  MAIN.c -o      MAIN.o 
gcc      MALLOC.o INPUT.o OUTPUT.o AVERAGE.o MAXADDRESS.o MAXNAME.o MAIN.o -o APP

二、Makefile的嵌套

(1)完整的程序
   #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
	char name[20];
	int age;
	int score[3];
	int average;
}STU;

STU *  MALLOC(int n,STU** st)
{
	*st = (STU *)malloc(sizeof(STU)*n);
	if(NULL == *st)
	{
		return NULL;
	}
	memset(*st,0,sizeof(STU)*n);
	return *st;
}

int INPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%s%*c%d",(st+i)->name,&((st+i)->age));
		for(j=0;j<3;j++)
		{
			scanf("%d",&((st+i)->score[j]));
		}
	}
}

int OUTPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		printf("%s  %d",(st+i)->name,((st+i)->age));
		for(j=0;j<3;j++)
		{
			printf("%d ",((st+i)->score[j]));
		}
	}
		printf("\n");
}

int AVERAGE(int n,STU*st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<3;j++)
		{
			(st+i)->average += ((st+i)->score[j]);
		}
		(st+i)->average = (st+i)->average/3;
		printf("%d\n",(st+i)->average);
		if((st+i)->average > 85)
		{
			printf("%s",(st+i)->name);
		}
	}
}

int MAXADDRESS(int n,STU * st)
{
	int i = 0;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(s->age < (st+i)->age)
		{
			s = st+i;
		}
	}
		printf("%p\n",s);
}

int MAXNAME(int n,STU * st)
{
	int i;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(strcmp(s->name,(st+i)->name)>0)
		{

		}else
		{
			s = st+i;
		}
	}
	printf("%s",s->name);
}

int main(int argc, const char *argv[])
{
	STU s;
	STU * stu = &s;
	MALLOC(3,&stu);
	printf("-----------\n");
	INPUT(3,stu);
	printf("-----------\n");
	OUTPUT(3,stu);
	printf("-----------\n");
	AVERAGE(3,stu);
	printf("-----------\n");
	MAXADDRESS(3,stu);
	printf("-----------\n");
	MAXNAME(3,stu);
	return 0;
}
(1.1)include文件夹
(1.1.1)stu.h
#ifndef _STU_H_
#define _STU_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct student
{
	char name[20];
	int age;
	int score[3];
	int average;
}STU;

STU * MALLOC(int n,STU**st);
int INPUT(int n,STU *st);
int OUTPUT(int n,STU *st);
int AVERAGE(int n,STU*st);
int MAXADDRESS(int n,STU *st);
int MAXNAME(int n,STU * st);
#endif
(1.2)src文件夹

address.c

 #include "../include/stu.h"
int MAXADDRESS(int n,STU * st)
{
	int i = 0;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(s->age < (st+i)->age)
		{
			s = st+i;
		}
	}
		printf("%p\n",s);
}

average.c

 #include "../include/stu.h"
int AVERAGE(int n,STU*st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<3;j++)
		{
			(st+i)->average += ((st+i)->score[j]);
		}
		(st+i)->average = (st+i)->average/3;
		printf("%d\n",(st+i)->average);
		if((st+i)->average > 85)
		{
			printf("%s",(st+i)->name);
		}
	}
}

input.c

#include "../include/stu.h"
int INPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%s%*c%d",(st+i)->name,&((st+i)->age));
		for(j=0;j<3;j++)
		{
			scanf("%d",&((st+i)->score[j]));
		}
	}
}

malloc.c

 #include "../include/stu.h"
STU *  MALLOC(int n,STU** st)
{
	*st = (STU *)malloc(sizeof(STU)*n);
	if(NULL == *st)
	{
		return NULL;
	}
	memset(*st,0,sizeof(STU)*n);
	return *st;
}

mian.c

 #include "../include/stu.h"

int main()
{
	STU s;
	STU *stu = &s;
	MALLOC(3,&stu);
	INPUT(3,stu);
	OUTPUT(3,stu);
}

name.c

 #include "../include/stu.h"
int MAXNAME(int n,STU * st)
{
	int i;
	STU *s = st;
	for(i=1;i<n;i++)
	{
		if(strcmp(s->name,(st+i)->name)>0)
		{

		}else
		{
			s = st+i;
		}
	}
	printf("%s",s->name);
}
   

output.c

 #include "../include/stu.h"
int OUTPUT(int n,STU * st)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		printf("%s  %d",(st+i)->name,((st+i)->age));
		for(j=0;j<3;j++)
		{
			printf("%d ",((st+i)->score[j]));
		}
	}
		printf("\n");
}

makefile

 All:mian.o input.o output.o malloc.o
	mv *.o ../obj/
mian.o:mian.c
	gcc -c mian.c -o mian.o
input.o:input.c
	gcc -c input.c -o input.o
output.o:output.c
	gcc -c output.c -o output.o
malloc.o:malloc.c
	gcc -c malloc.c -o malloc.o

(1.3)obj文件夹

input.o

malloc.o

mian.o

output.o

makefile

All:App
	mv App ../bin/
App:mian.o input.o output.o
	gcc *.o -o App

(1.4)bin文件夹

APP

(1.5)Makefile
All:
	make -C ./src/
	make -C ./obj/
.PHONY:
clean:
	rm ./obj/*.o
	rm ./bin/App

(2)执行

运行:rm ./obj/*.o # 删除所有 *.o 文件

运行:rm ./bin/App # 删除可执行文件APP

(3)结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值