Makefile 目标 cd 到一个目录并运行一个 go 应用程序

我需要 cd 到特定目录,然后简单地运行go run main.go并启动我的应用程序。我有以下内容:


stand-app: |

    $(info Booting up the app for local testing)

    cd $(PWD)/cmd/myapp

    go run main.go

当我运行目标时,我得到以下信息:


go run main.go

stat main.go: no such file or directory

make: *** [stand-app] Error 1

但是位置是正确的,我可以简单地通过 cd 到该目录并运行命令来执行这些步骤。我哪里错了?


谢谢


手掌心
浏览 306回答 1
1回答

人到中年有点甜

配方中的每个逻辑行都在不同的 shell 中运行。当前工作目录是 shell 的一个属性;当 shell 消失时,对工作目录的任何更改也会消失。所以,你cd ...是一个空操作:它改变了那个 shell 中的工作目录,然后 shell 退出,下一个 shell 从原来的工作目录开始。将这两行合并为一条逻辑行;也许是这样的:stand-app:         $(info Booting up the app for local testing)         cd $(PWD)/cmd/myapp && go run main.go只是提醒一下:PWD制作起来并不特别。如果您从中调用 make 的 shell 设置了它,它将有一个值,如果您从中调用 make 的 shell 没有设置它,它将没有值(或者如果它在父 shell 中设置不正确,它将有错误的值). 您可能想改用 make 的CURDIR变量:stand-app:         $(info Booting up the app for local testing)         cd '$(CURDIR)/cmd/myapp' && go run main.go
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go