makefile浅谈

具体讲解

官方讲解 http://www.gnu.org/software/make/manual/make.html

个人理解:简单的说  makefile的产生源于需要对大型项目的文件依赖关系的解决。 它可以解决重复使用编译指令和每次都要处理文件依赖的麻烦。

gcc -c表示只编译(compile)源文件但不链接,会把.c或.cc的c源程序编译成目标文件,一般是.o文件。
gcc -o用于指定输出(out)文件名。不用-o的话,一般会在当前文件夹下生成默认的a.out文件作为可执行程序。//对于多个文件,需要用.o文件作为参数,无法直接使用.c文件。

假如我有如下几个文件:

      head.c  head.h  main.c

//head.h
#include <stdio.h>

#include <stdlib.h>

#define MAXN 1005

struct node{

   int x, y;

};

void print();

void scan();

//head.c
#include "head.h"

void print()

{

    printf("print()\n");

}

void scan()

{

    int x;

    scanf("%d", &x);

    printf("%d\n", x);

}
//main.c
#include "head.h"

int main()

{

    struct node c;

    printf("hello\n");

    scan();

    print();

}

不用makefile的写法 :

gcc -c head.h head.c main.c -o test

放入makefile文件中

objects = head.o main.o

test : $(objects)
	gcc -o test $(objects) 
head.o: head.c head.h
	gcc -c head.c head.h
main.o: head.h main.c
	gcc -c head.h main.c
.empty : clean
clean :
	-rm -rf $(objects) test

objects 里放gcc -o 要用到的文件 

$表示前面宏定义的展开

生成的文件名写在前面, 每一个第二行的生成指令注意前面要有TAB键缩进

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值