比如我们想要在makefile中写脚本去执行shell的ls命令看一下文件有没有生成,可以这样用
方式1
ls:=$(shell ls -a)
all:
@echo $(ls)
也就是说可以这样执行
变量名 :=$(shell 命令名)
all:
@echo $(变量名)
echo 就是把结果打印出来
方式二(这个常用)
all:
@shell命令 参数
# 比如
all:
@ls -a
注意有的命令不可以直接用比如source
shell脚本里面执行source命令提示错误
在Makefile中source脚本
在做项目时,通常会用到Makefile,并且很多脚本可以重复使用。因此,若在Makefile中source已有的脚本,则方便很多。
然而直接在Makefile中使用source XX.sh,则terminal显示“make : source:command not found”。一脸懵逼呀!在terminal中输入source xx.sh,正常运行。接下来针对此问题提出解决方法。
原因:makefile的默认shell是/bin/sh,本身不支持source。
原问题
这个SHELL变量默认是 SHELL :=/bin/sh ,这个命令里面没有source命令
uart_test: uart_test.c
@source /opt/fsl-imx-x11/4.1.15-2.1.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
${CC} $^ -o uart_test
解决办法
修改SHELL,使其包好source命令
这样就不会报错
SHELL:=/bin/bash
uart_test: uart_test.c
@source /opt/fsl-imx-x11/4.1.15-2.1.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
${CC} $^ -o uart_test