Makefiles:使用 'pure make syntaxis' 读取文件(无 shell 命令)

Makefiles: reading a file with 'pure make syntaxis' (no shell commands)

我想读取一个文本文件的内容并将其分配给我的 makefile 中的一个变量。我阅读了 make 手册 ch 8.6: The file function,并在我的 makefile 中写了这个简单的片段:

# Snippet from my makefile

my_var = $(file < C:/full/path/to/my_file.txt)

all:
    @echo $(my_var)

我收到以下错误:

*** Invalid file operation: < C:/full/path/to/my_file.txt.  Stop.

我特意选择 $(file ..) 而不是 $(shell ..) 函数来读出文件。那是因为我的 makefile 应该尽可能独立于平台。 $(file ..) 函数是纯 makefile 语法,而 $(shell ..) 函数包含 shell 语法。

我做错了什么?

如果您想使用 $(file <).

读取文件,您至少需要升级到 make 4.2

来自http://git.savannah.gnu.org/cgit/make.git/tree/NEWS

Version 4.2 (22 May 2016)

  • The $(file ...) function can now read from a file with $(file <FILE). The function is expanded to the contents of the file. The contents are expanded verbatim except that the final newline, if any, is stripped.