学习了一下makefile的写法,写了一个比较简单的makefile。
学习参考http://www.cnblogs.com/OpenShiFt/p/4313351.html/
makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| CC = gcc CFLAGS += LDFLAGS += EXEC = main OBJS = main.c function.c
all: $(EXEC)
.PHONY: all
main: $(OBJS) $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
clean: -rm -f $(EXEC) *.elf *.gdb *.o *.a
.PHONY: clean
|
function.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <stdio.h>
#define MAX 5
int array[MAX] = {2,7,6,4,8};
int sum(){
int i; int n=0;
for(i=0;i<MAX;i++) n+=array[i]; return n; }
int max(){ int i=0; int max;
max=array[i]; for(i=0;i<MAX;i++){ if(array[i] > max) max = array[i]; }
return max; }
|
include.h
1 2 3 4 5 6 7 8 9 10
| #include <stdio.h>
#define len 5
void print(int array[]){ int i; for(i=0;i<len;i++) printf("array[%d] : %d\n",i,array[i]); }
|
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h> #include "include.h"
extern int array[]; extern int sum(); extern int max();
int main(void){ int sum_m,max_m; sum_m = sum(); max_m = max(); print(array); printf("sum: %d , max: %d\n",sum_m,max_m);
return 0; }
|