makefile编译helloword
hello.c文件:
#include <stdio.h>
int main() {
printf("Hello Word!!!\n");
return 0;
}
Makefile文件:
执行顺序,
1.hello:hello.o中的hello是要生成的执行文件,谁来生成呢? 就是hello.o。
2.如果没有hello.o文件呢?make就会自动在文件里寻找依赖关系(自动推导),
看hello.o怎么生成的,就是“hello.o:hello.c”。
3.以此类推的一个递归。
4.找到依赖关系就会执行下面的cc那条语句
注意:1.hello:hello.o和hello.o:hello.c 的位置不可互换,
make默认把第一个target做为生成第一目标,也就是hello:hello.o里的hello(第一目标或者可以理解为makefile的入口)
2.Makefile执行如果报错,可能就是行的缩进问题,Makefile这点很像Python
#first makefile
hello:hello.o
cc -o hello hello.o
hello.o:hello.c
cc -c -o hello.o hello.c
clean :
@echo " "
ifeq (,$(wildcard ./hello))
@echo "'hello' file is not exist."
else
@echo "'hello' file is exist."
rm hello
endif
@echo " "
ifeq (,$(wildcard ./hello.o))
@echo "'hello.o' file is not exist."
else
@echo "'hello.o' file is exist."
rm hello.o
endif
@echo "\nClean done."
执行make编译(make 会自动寻找当前目录下名字为Makefile名字的文件)
$ make
下图是执行make编译完成后,产生了hello可执行文件
(下图绿色的hello为可执行文件,没错,Linux下可执行文件不需要有后缀)

执行make clean命令:
$ make clean
执行了rm 删除文件

为什么Makefile里用CC ,而命令行用gcc?
答:(这里引用网上的一段话)gcc是C编译器;g++是C++编译器;linux下cc一般是一个符号连接,指向gcc;gcc和g++都是GUN(组织)的编译器。而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Linux/Unix都是大小写敏感的系统,这点一定要注意。
参考:
浙公网安备 33010602011771号