0

我正在尝试检测 MFLAGS 中的用户设置(什么样的 var 类型)并添加适当的其他选项(printf 格式):

CC:=gcc
CFLAGS:=-g3 -Wall -pedantic -std=c99
LDFLAGS:=
MFLAGS:=-Dcost_type="int"  < default MFLAGS
SOURCES:=...
OBJECTS:=$(SOURCES:.c=.o)
EXECUTABLE:=...

ifneq (,$(findstring "-Dcost_type=\"int\"",$(MFLAGS)))
        MFLAGS:="$(MFLAGS) -Dcost_fmt=\"%d\""
endif
ifneq (,$(findstring "double",$(MFLAGS)))
        MFLAGS:="$(MFLAGS) -Dcost_fmt=\"%f\""
endif
...

但是该示例对任何输入都没有反应:

make MFLAGS:="-Dcost_type=\"int\""
make MFLAGS:="-Dcost_type=\"double\""
4

2 回答 2

2

这里有几个问题,

  1. 您将需要使用“覆盖”从命令行更改变量

  2. 不要引用传递给 findstring 的字符串

改写成这样的东西,

ifneq (,$(findstring -Dcost_type="int",$(MFLAGS)))
override MFLAGS += " -Dcost_fmt=\"%d\""
endif
ifneq (,$(findstring double,$(MFLAGS)))
override MFLAGS += " -Dcost_fmt=\"%f\""
endif
于 2012-04-29T20:19:35.073 回答
0

我不得不再次寻求帮助。我写了那个makefile,它适用于“int”,但不适用于“short”(为什么?)。那么空间呢(例如long long),因为当我这样做时它使用“long”而不是“long long”:make MFLAGS=-Dcost_type="\"long long\""

ifneq (,$(findstring short,$(MFLAGS)))
        override MLAGS+=-Dcost_fmt=\"%hd\" -Dcost_max=SHRT_MAX
endif
ifneq (,$(findstring unsigned short,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%hd\" -Dcost_max=USHRT_MAX
endif
ifneq (,$(findstring int,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%d\" -Dcost_max=INT_MAX
endif
ifneq (,$(findstring unsigned int,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%d\" -Dcost_max=UINT_MAX
endif
ifneq (,$(findstring long,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%ld\" -Dcost_max=LONG_MAX
endif
ifneq (,$(findstring unsigned long,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%ld\" -Dcost_max=ULONG_MAX
endif
ifneq (,$(findstring long long,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%lld\" -Dcost_max=LLONG_MAX
endif
ifneq (,$(findstring unsigned long long,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%lld\" -Dcost_max=ULLONG_MAX
endif
ifneq (,$(findstring float,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%f\" -Dcost_max=FLT_MAX
endif
ifneq (,$(findstring double,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%f\" -Dcost_max=DBL_MAX
endif
ifneq (,$(findstring long double,$(MFLAGS)))
        override MFLAGS+=-Dcost_fmt=\"%Lf\" -Dcost_max=LDBL_MAX
endif
于 2012-05-01T16:01:16.807 回答