• Makefile学习笔记


    一、查看make命令的位置

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

    二、语法

    (一)标签式语法

    标签:#顶格写
    按Tab键指令 #Tab键开头
    
    • 1
    • 2
    • 例子:
    rm a.out
    touch Makefile
    vi Makefile
    
    cat Makefile
    hqyj1:
    	gcc k1.c -o a.out
    hqyj2:
    	gcc k2.c -o b.out
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 直接使用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
    
    • 1
    • 2
    • 3
    • 4
    • 如果当前路径下有多个Makefile文件,在终端使用命令 make -f 文件 来指定执行哪个Makefile文件
    linux@ubuntu:~/DC23071/m8d22$ make -f Makefile hqyj2
    gcc k2.c -o b.out
    linux@ubuntu:~/DC23071/m8d22$ ./a.out 
    
    • 1
    • 2
    • 3
    linux@ubuntu:~/DC23071/m8d22$ ./b.out 
    hello
    
    • 1
    • 2

    (二)依赖式语法

    • 目录:依赖 #顶格写
    • 按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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    MQTT协议
    14.0_[Java 异常]-异常以及异常处理机制
    Linux 必会基础语句 软硬连接区别 Linux文件类型
    高级深入--day22
    使用人工智能聊天机器人时要注意这些!(配提问技巧)
    元宇宙,小荷才露尖尖角
    C++文档阅读笔记-Core Dump (Segmentation fault) in C/C++
    mac os 渗透测试常用命令
    485modbus转profinet网关在混料配料输送系统应用博图配置案例
    数据结构之单链表
  • 原文地址:https://blog.csdn.net/qq_41878292/article/details/138133450