Makefile使用

在Shell脚本中使用make命令来进行编译,尤其在C开发中,make命令通过makefile文件中描述源程序之间的依赖关系进行自动编译;makefile文件是按照规定格式编写,需说明如何编译各个源文件并连接生成可执行文件,并要求定义源文件之间的依赖关系;很多大型项目的编译都是通过 Makefile 来组织的。

首先查看是否安装了 make

make -v

 出现:

GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

 

代表安装成功了。

清理一下.o文件:

rm *.o

 

然后打开Makefile 注意首字母要大写

vi Makefile

然后写入依赖关系

#this is make fike 代表注释内容
a.out:2.o 3.o 1.c #可执行文件:由哪些文件依赖
        gcc 2.o 3.o 1.c -o main.out #执行gcc命令 一定要用编译器里的Tab否择会出错
2.o:2.c #目标代码文件:由.c文件生成
        gcc -c 2.c
3.:3.c
        gcc -c 3.c

 

保存退出,执行make

dacui@ubuntu:~/Desktop/study$ make
gcc -c 2.c
cc    -c -o 3.o 3.c
gcc 2.o 3.o 1.c -o main.out

 

ls查看下文件

1.c  2.c  2.h  2.o  3.c  3.h  3.o  a.out  main.out  Makefile

 

执行a.out /main.out

dacui@ubuntu:~/Desktop/study$ ./main.out
num=:10 min=1
dacui@ubuntu:~/Desktop/study$ ./a.out
num=:10 min=1

结果是一样的

 

posted @ 2020-05-02 19:04  北ღ冥  阅读(174)  评论(0编辑  收藏  举报