makefile - 在 Makefile 中加载 .env 文件

标签 makefile

是否有一种简单的方法可以将包含本地配置的文件自动加载到 Makefile 中? ?

我有一个文件,.env ,包含 key=value每行成对:

var1=val1
var2=val2

目前我正在将每个变量导入我的 Makefile像这样手动:
  var1=$$(grep '^var1=' .env | cut -d= -f2-)

当添加更多变量时,这感觉很笨拙并且容易出错。

我还尝试向 Makefile 添加一个额外的目标。阅读 .env 的每一行并将其传递给 export ,但在一个目标中导出的值对其他目标不可用。

我希望在 make 中有一个内置功能可以执行此操作,但我一直无法找到它的文档。

最佳答案

假设您的包含文件完全由有效的 make 分配组成,那么 include directive很可能是你想要的。

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this:

include filenames…

filenames can contain shell file name patterns.

关于makefile - 在 Makefile 中加载 .env 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30300830/

相关文章:

c++ - 使用 make 构建我的项目

c++ - 更新代码后,从命令行运行 eclipse 生成的 makefile 不起作用

使用 make 编译 allegro5 程序

c - Makefile...创建一个静态库

c - 库中未实现函数的 Makefile

c++ - ld : library not found for -lbitcoin

linux - 创建 Docker 镜像时 Makefile 错误

c++ - 提高从 qmake 生成的 Makefile 性能

c++ - 使用 Armadillo 矩阵库时用于 C++ 代码的 Makefile

C++ cmake/制作 : linker ld does not find library (missing path? )