问题
I have an impression that I can call bash function inside GNU makefile, but seems wrong. Here is a simple test, I have this function defined:
>type lsc
lsc is a function
lsc ()
{
ls --color=auto --color=tty
}
Here is my Makefile:
>cat Makefile
all:
lsc
Here is what I get in running make:
>make
lsc
make: lsc: Command not found
make: *** [all] Error 127
Is my impression wrong? Or is there any env setup issue? I can run "lsc" at the command line.
回答1:
You cannot call bash functions or aliases in a Makefile, only binaries and scripts. What you can do however, is calling an interactive bash and instruct it to call your function or alias:
all:
bash -i -c lsc
if lsc is defined in your .bashrc, for example.
回答2:
Use $* in your BASH script:
functions.sh
_my_function() {
echo $1
}
# Allows to call a function based on arguments passed to the script
$*
Makefile
test:
./functions.sh _my_function "hello!"
Running example:
$ make test
./functions.sh _my_function "hello!"
hello!
回答3:
Did you export your function with "export -f"?
Is bash the shell of your Makefile, or is is sh?
回答4:
You can import all shell scripts functions from a shell file if you use this from the question How do I write the 'cd' command in a makefile?
.ONESHELL: my_target
my_target: dependency
. ./shell_script.sh
my_imported_shell_function "String Parameter"
If you like, you can also even do not use the .ONESHELL thing and do it all in one line just by using a colon ; right after the importing the shell script:
my_target: dependency
. ./shell_script.sh; my_imported_shell_function "String Parameter"
来源:https://stackoverflow.com/questions/13109242/cannot-call-bash-function-inside-makefile