Makefile学习笔记

发布于:2024-04-25 ⋅ 阅读:(20) ⋅ 点赞:(0)

一、查看make命令的位置

ls -l /usr/bin/make
系统没有的话,手动安装:
sudo apt-get install make

二、语法

(一)标签式语法

标签:#顶格写
按Tab键指令 #Tab键开头
  • 例子:
rm a.out
touch Makefile
vi Makefile

cat Makefile
hqyj1:
	gcc k1.c -o a.out
hqyj2:
	gcc k2.c -o b.out
  • 直接使用make命令执行, 默认从当前路径找到名字为Makefile或者makefile执行, 默认执行的是第一个标签下的指令
    使用make 标签名 指定哪个标签下的指令
linux@ubuntu:~/DC23071/m8d22$ make
gcc k1.c -o a.out
linux@ubuntu:~/DC23071/m8d22$ make hqyj2
gcc k2.c -o b.out
  • 如果当前路径下有多个Makefile文件,在终端使用命令 make -f 文件 来指定执行哪个Makefile文件
linux@ubuntu:~/DC23071/m8d22$ make -f Makefile hqyj2
gcc k2.c -o b.out
linux@ubuntu:~/DC23071/m8d22$ ./a.out 
linux@ubuntu:~/DC23071/m8d22$ ./b.out 
hello

(二)依赖式语法

  • 目录:依赖 #顶格写
  • 按Tab键指令 #Tab键开头
  • 例子:
a.out:k1.c
	gcc k1.c -o a.out
linux@ubuntu:~/DC23071/m8d22$ vi makefile2
linux@ubuntu:~/DC23071/m8d22$ cat makefile2
a.out:k1.c
	gcc k1.c -o a.out
linux@ubuntu:~/DC23071/m8d22$ make -f makefile2
  • make: “a.out”已是最新。 Makefile会根据时间戳判断哪些文件需要重新编译的功能
linux@ubuntu:~/DC23071/m8d22$ rm a.out b.out Makefile makefile2
linux@ubuntu:~/DC23071/m8d22$ cat Makefile 
a.out:test.o
	gcc test.o -o a.out
test.o:test.s
	gcc -c test.s -o test.o
test.s:test.i
	gcc -S test.i -o test.s
test.i:test.c
	gcc -E test.c -o test.i
linux@ubuntu:~/DC23071/m8d22$ make
gcc -E test.c -o test.i
gcc -S test.i -o test.s
gcc -c test.s -o test.o
gcc test.o -o a.out
linux@ubuntu:~/DC23071/m8d22$ make
  • make: “a.out”已是最新
  • Makefile中使用#表示注释,且只有单行注释 如果想用多行注释,可以使用连行符 \ 指令前加@可以取消指令的回显
a.out:test.o
    @gcc test.o -o a.out
test.o:test.s
    @gcc -c test.s -o test.o
test.s:test.i
    @gcc -S test.i -o test.s
test.i:test.c
    @gcc -E test

网站公告

今日签到

点亮在社区的每一天
去签到