Create a variable in a makefile by reading content

2019-01-22 10:57发布

How can I create a variable on-the-fly from makefile, the value of which would be the entire contents of another data file.

标签: makefile
7条回答
Emotional °昔
2楼-- · 2019-01-22 11:15

Much simpler since $(file op filename) was added:

VARIABLE = $(file < my_file.txt)

Manual page here: https://www.gnu.org/software/make/manual/html_node/File-Function.html#index-file_002c-reading-from

查看更多
Fickle 薄情
3楼-- · 2019-01-22 11:27

As the platform has not been specified, this is the way how it works on Solaris:

VERSION:sh = cat VERSION

all:
        echo $(VERSION)
查看更多
做个烂人
4楼-- · 2019-01-22 11:28

cat doesn't exist on Windows. Solution that works for Linux and Windows:

cat := $(if $(filter $(OS),Windows_NT),type,cat)
variable := $(shell $(cat) filename)

Explanation: Seems like On Windows there is always OS environment variable defined to be equal to 'Windows_NT'. This way, for Windows type command is used, for non-Windows cat is used.

查看更多
\"骚年 ilove
5楼-- · 2019-01-22 11:29

GNU make version 4.2 supports file reading operation, so with respect to Maxim Egorushkin's great answer there is an optional way to solve this problem now:

FILE     := test.txt
variable :=$(file < $(FILE))
查看更多
做自己的国王
6楼-- · 2019-01-22 11:33

If you are using GNU make, another way to get this effect is to use a make "include" of another makefile:

From Makefile:

include "./MyFile.mak"

Create a file "MyFile.mak" with content:

FILE := "my file content"
FILE += "more content 1"
FILE += "more content 2"
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-22 11:35

Assuming GNU make:

file := whatever.txt
variable := $(shell cat ${file})
查看更多
登录 后发表回答