zoukankan      html  css  js  c++  java
  • [GNU] 喝一杯咖啡, 写一写 Makefile

    Makefile 是 Linux 下组织程序的一个工具,它的命令是 make。

    首字母M/m都可以

    【Makefile】

    Makefile 编写的主旋律:

    target: [dependency]
    (TAB)[command]

    【make】

    了解支持的功能和选项:

    $ man make     # 查看完整手册
    $ make --help  # 快速查看格式和选项

    用法示例:

    # 指定 target 是 all; 指定 Makefile 文件为 build.mk; -s 表示不输出任何系统提示.
    $ make all -f build.mk -s

    $
    make [target] # 默认就是读取 Makefile,默认 target 是文件内首个 target 

    【流程】

    make 命令读取 Makefile 和 target;

    检查 target 的依赖是否有更新,有就执行 command,没有就提示目标文件已是最新。

    一个统计 controbuter 的 Makefile:

    usage = "
    Usage: make <option> authors"
    
    default:
        @echo $(usage)
    
    authors:
        @git log --format='%aN <%aE>' | sort -u > $@

    @    不在终端显示执行的命令,等同于 make 加 -s 选项。

    $@  等同于当前 target。

    $^   当前 target 的所有依赖。

    $<   当前 target 依赖中的第一个。

    |      shell 的管道符。

    >     shell 的覆盖写入标记。

    【变量使用】

    引用式(=),相互影响:

    jack = $(mike)
    mike = $(lucy)
    lucy = 18
    
    default:
        @echo $(jack) # 18
        @echo $(mike) # 18
        @echo $(lucy) # 18

    除了赋值之外,特性跟 shell 完全不一样;shell 的等号两边是不允许空格的,且不是引用式的。 

    展开式(:=),只取前面变量的值:

    jack := $(mike)
    mike := $(lucy)
    lucy = 18
    lucy ?= 19
    bob = 20
    bob += 16
    default: @echo $(jack) # 空
    @echo $(mike) # 空 @echo $(lucy) # 18
    @echo $(bob) # 20 16

      

    变量为空时才进行赋值(?=).

    值追加到变量末尾(+=).

    【伪目标】

    hello: 
        touch hellomake
    
    #.PHONY: hellomake hellomake:
    echo "this is hellomake target."

    clean:
    rm -f hellomake

    分析一下上面的文件:

    make hello 或 make;不指定 target 默认是 hello,执行 `touch hellomake`.

    make hellomake;提示 make: `hellomake' is up to date. 

    显然,因为存在 hellomake 这个文件的存在,make hellomake 得到了非预期效果,为了避免这类冲突,需要排除此 target 成为目标文件;

    打开注释 .PHONY: hellomake 用来标示伪目标,然后再执行上面命令就是执行`rm -f hellomake`.

    注意当 hellomake 文件不存在时,make hellomake 是可以执行它下面的命令的。

    【补充】

    Makefile 隐含规则

    A Simple Makefile Tutorial

    GNU make 

    Autoconf 

    Link : http://www.cnblogs.com/farwish/p/6148023.html

  • 相关阅读:
    短信发送流程
    aidl
    tail
    RIL层传输的方式就是socket
    adb s <设备> <命令>
    Shell
    你好,色彩 android:background="@color/white" [create file color.xml at res/values/]
    [C#]在C#中使用NUnit进行单元测试
    [ASP.NEt] IE6布署NET网站时,Oracle 抛出异常
    [ASP.NET]如何Response.Redirect新的页面到指定的框架中(原创)
  • 原文地址:https://www.cnblogs.com/farwish/p/6148023.html
Copyright © 2011-2022 走看看