代码之家  ›  专栏  ›  技术社区  ›  Goles

将makefile变量值赋给bash命令结果?

  •  37
  • Goles  · 技术社区  · 16 年前

    我正在尝试将此命令的输出(在我的makefile中)分配给makefile header var,如下代码行所示:

    HEADER = $(shell for file in `find . -name *.h`;do echo $file; done)
    

    问题是,如果我在makefile中使用以下方法打印头文件:

    print:
        @echo $(HEADER)
    

    我得到

    ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile
    

    如果我直接在控制台中运行这个命令,并直接在makefile所在的位置运行:

    myaccount$ for file in `find . -name *.h`;do echo $file; done
    ./engine/helper/crypto/tomcrypt/headers/._tomcrypt_pk.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_argchk.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_cfg.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_cipher.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_custom.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_hash.h
    ./engine/helper/crypto/tomcrypt/headers/tomcrypt_mac.h
    ....
    

    所以我得到了所有的头文件。我这样做是为了避免在makefile中手动指定所有.h文件。

    有什么想法吗?

    3 回复  |  直到 12 年前
        1
  •  66
  •   James Eichele Bernard Igiri    16 年前

    你需要双重逃离 $ shell命令中的字符:

    HEADER = $(shell for file in `find . -name *.h`;do echo $$file; done)
    

    这里的问题是make将尝试扩展 $f 作为一个变量,由于它找不到任何内容,所以它只是用“”替换它。你的shell命令只剩下 echo ile 它忠实地做到了。

    添加 $$ 告诉Make放置一个 $ 在这个位置上,shell命令将按照您希望的方式显示。

        2
  •  14
  •   sorpigal    16 年前

    为什么不简单地做呢

    HEADER = $(shell find . -name '*.h')
    
        3
  •  7
  •   BЈовић    12 年前

    这个 makefile tutorial 建议使用 wildcard 获取目录中的文件列表。在您的案例中,它意味着:

    HEADERS=$(wildcard *.h)