makefile使用
土豆 2023/3/16
# GCC编译
查看gcc版本号
gcc --version
# 显示规则
# 简单编译
hello.c
需要编译的文件
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
Makefile
文件,编译过程是从下往上
hello.exe:hello.o
gcc hello.o -o hello
hello.o:hello.S
gcc -c hello.S -o hello.o
hello.S:hello.i
gcc -S hello.i -o hello.S
hello.i:hello.c
gcc -E hello.c -o hello.i
编译过程
PS D:\> make
gcc -E hello.c -o hello.i
gcc -S hello.i -o hello.S
gcc -c hello.S -o hello.o
gcc hello.o -o hello
PS D:\project\HelloWorld>
# 伪目标
.PHONY:
关键字可以定义伪目标
.PHONY:
clearall:
del hello.o hello.S hello.i hello.exe
clear:
del hello.o hello.S hello.i
通过执行make clearall
就可以删除所有编译的文件
# 变量定义
=
替换 +=
追加 :=
常量
不使用变量编译多个文件
test:circle.o cube.o main.o
gcc circle.o cube.o main.o -o test
circle.o:circle.c
gcc -c circle.c -o circle.o
cube.o:cube.c
gcc -c cube.c -o circle.o
main.o:main.c
gcc -c main.c -o main.o
使用变量
TAR = test
OBJ = circle.o cube.o main.o -o
CC := gcc
$(TAR):$(OBJ)
$(CC) $(OBJ) -o $(TAR)
circle.o:circle.c
$(CC) -c circle.c -o circle.o
cube.o:cube.c
$(CC) -c cube.c -o circle.o
main.o:main.c
$(CC) -c main.c -o main.o
# 隐含规则
%.o
任意的.o
文件,%.c
任意的.c
文件
TAR = test
OBJ = circle.o cube.o main.o -o
CC := gcc
$(TAR):$(OBJ)
$(CC) $(OBJ) -o $(TAR)
%.o:%.c
$(CC) -c %.c -o %.o