Assign a makefile variable value to a bash command

2019-01-13 17:42发布

I'm trying to assign the output of this command ( that is in my makefile ) to the makefile HEADER var like in this following line of code:

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

The problem is that if I print HEADER in my makefile using:

print:
    @echo $(HEADER)

I get

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

And if I run this command directly in the console, and directly where my makefile is:

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
....

So I get all my header files. I'm doing this to avoid manually specifying all my .h files manually in my makefile.

Any ideas ?

3条回答
时光不老,我们不散
2楼-- · 2019-01-13 18:09

The makefile tutorial suggested to use wildcard to get list of files in a directory. In your case, it means this :

HEADERS=$(wildcard *.h)
查看更多
Melony?
3楼-- · 2019-01-13 18:10

You will need to double-escape the $ character within the shell command:

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

The problem here is that make will try to expand $f as a variable, and since it doesn't find anything, it simply replaces it with "". That leaves your shell command with nothing but echo ile, which it faithfully does.

Adding $$ tells make to place a single $ at that position, which results in the shell command looking exactly the way you want it to.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-13 18:15

Why not simply do

HEADER = $(shell find . -name '*.h')
查看更多
登录 后发表回答