简单的makefile编写

首先我们给定三个文件:main_plus.c, plus.c plus.h

main_plus.c

/*************************************************************************
	> File Name: main_plus.c
	> Author: ahuang1900
	> Mail: ahuang1900@qq.com 
	> Created Time: 2014年10月04日 星期六 20时27分53秒
 ************************************************************************/

#include<stdio.h>
#include "plus.h"

int main(void)
{
	int a = 0, b = 0;
	printf("Please enter integer a:");
	scanf("%d",&a);
	printf("\nPlease enter integer b:");
	scanf("%d",&b);
	if ( a>b) {
		printf("\nThe sum is %d\n", plus(b,a));
	} else {
		printf("\nThe sum is %d\n", plus(a,b));
	}
	return 0;
}

plus.c

/*************************************************************************
	> File Name: plus.c
	> Author: ahuang1900
	> Mail: ahuang1900@qq.com 
	> Created Time: 2014年10月04日 星期六 20时34分18秒
 ************************************************************************/

#include<stdio.h>
#include "plus.h"
int plus(int a, int b)
{
	int sum = a;
	int i;
	for (i = a+1; i<=b; i++)
		sum += i;
	return sum;
}

plus.h

/*************************************************************************
	> File Name: plus.h
	> Author: ahuang1900
	> Mail: ahuang1900@qq.com 
	> Created Time: 2014年10月04日 星期六 20时32分02秒
 ************************************************************************/

#ifndef _PLUS_H_
#define _PLUS_H_
#include <stdio.h>
int plus(int a, int b);
#endif


如果不编写makefile,通常我们的做法是:

hellen@hellen1900:~/myjob/makefile$ ls
main_plus.c  makefile  plus.c  plus.h
hellen@hellen1900:~/myjob/makefile$ gcc -c main_plus.c
hellen@hellen1900:~/myjob/makefile$ gcc -c plus.c
hellen@hellen1900:~/myjob/makefile$ ls
main_plus.c  main_plus.o  makefile  plus.c  plus.h  plus.o
hellen@hellen1900:~/myjob/makefile$ gcc -o main plus.o main_plus.ohellen@hellen1900:~/myjob/makefile$ ls
main  main_plus.c  main_plus.o  makefile  plus.c  plus.h  plus.o
hellen@hellen1900:~/myjob/makefile$ ./main
Please enter integer a:1

Please enter integer b:2

The sum is 3
hellen@hellen1900:~/myjob/makefile$ 

因此我们可以编写makefile如下:

版本1:

main: main_plus.o plus.o
	gcc -o main main_plus.o plus.o

main_plus.o: main_plus.c plus.h
	gcc -c main_plus.c
plus.o: plus.c plus.h

clean:
	rm -f *.o main

版本2:

#$@:目标文件
#$^:所有的依赖文件
#$<:第一个依赖文件
main:main_plus.o plus.o
	gcc -o $@ $^

main_plus.o: main_plus.c plus.h
	gcc -c $<

plus.o: plus.c plus.h
	gcc -c $<

clean:
	rm -f *.o main


版本3:

object = main_plus.o plus.o

main: $(object)
	gcc -o $@ $(object)

main_plus.o: main_plus.c
plus.o: plus.c

clean:
	rm -f *.o main

版本4:

CC=gcc
CFLAG = -g -Wall
object = main_plus.o plus.o

main : $(object)
	$(CC) $(CFLAG) -o $@ $^

main_plus.o : main_plus.c

plus.o : plus.c

%.o:%c
	$(CC) $(CFLAG) -c -o $@ $<

clean:
	rm -f *.o main


参考:http://blog.csdn.net/xj626852095/article/details/37879217


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值