linux下makefile使用

make 是一个解释Makefile 文件中指令的命令工具,其最基本的功能就是通过Makefile文件来描述源程序之间的相互关系并自动维护编译工作,它会告知系统以何种方式编译和链接程序。

Makefile写好之后,每次改变了某些源文件,只要执行make命令:#make

所有必要的重新编译将执行。Make程序利用makefile中的数据和每个文件的最后修改时间来确定那个文件需要更新,对于需要更新的文件,make程序执行makefile数据中定义的命令来更新。

假定一个项目中有以下一些文件。

源程序:Main.ctest1.ctest.c

包含的头文件:head1.hhead2.hhead3.h

由源程序和头文件编译生成的目标文件:Main.otest1.otest2.o

由目标文件链接生成的可执行文件:test



makefile文件例子

test:main.o test1.o test2.o

gcc -o test main.o test1.o test2.o

main.o:main.c head1.h head2.h

gcc -c main.c

test2.o:test2.c head2.h

gcc -c test2.c

test1.o:test1.c head1.h

gcc -c test1.c

install:

cp test /home/xwj/Desktop/makefile/tmp

clean:

rm -f *.o

main.c

#include <stdio.h>

#include "head2.h"

int main()

{

   printf("This is main fun.\n");

   outputHead2();

   return 0;

}

test1.c

#include "head1.h"

void outputHead1()

{

    printf("\nThis is head1.\n");

}

test2.c

#include "head2.h"

void outputHead2()

{

    printf("\nThis is head2.\n");

outputHead1();

}

head1.c

#ifndef HEAD1_H

#define HEAD1_H

#include <stdio.h>

void outputHead1();

#endif

head2.c

#ifndef HEAD1_H

#define HEAD1_H

#include <stdio.h>

#include "head1.h"

void outputHead2();

#endif

输入命令make就会执行makefile文件

输入make install或者make clean就会执行在makefile写好的相应命令。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值