Makefile学习笔记

一、查看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

相关推荐

  1. Makefile学习笔记

    2024-04-23 23:00:02       29 阅读
  2. Makefile学习

    2024-04-23 23:00:02       36 阅读
  3. Linux-笔记 Makefile简单入门

    2024-04-23 23:00:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 23:00:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 23:00:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 23:00:02       87 阅读
  4. Python语言-面向对象

    2024-04-23 23:00:02       96 阅读

热门阅读

  1. C语言程序每日一练(9、楼梯)

    2024-04-23 23:00:02       29 阅读
  2. C++ 脚本处理代码记录

    2024-04-23 23:00:02       32 阅读
  3. 4月23日加油站+分发糖果

    2024-04-23 23:00:02       36 阅读
  4. 冒泡排序算法及其Python实现

    2024-04-23 23:00:02       34 阅读
  5. 程序员如何搞副业?

    2024-04-23 23:00:02       40 阅读
  6. 书生浦语训练营第2期-第5节笔记

    2024-04-23 23:00:02       37 阅读
  7. 类的声明与成员函数的实现--Car类

    2024-04-23 23:00:02       36 阅读
  8. C++ 核心编程 - 函数提高

    2024-04-23 23:00:02       41 阅读
  9. Gitea:轻量级、开源的Git仓库管理平台

    2024-04-23 23:00:02       35 阅读