ifdef命令未找到(GNU make 3.81)

6

我有一个简单的helloWorld程序,下面是它的makefile

helloWorld: helloWorld.cpp
        echo "Argument entered is $(foo)"
        ifdef foo
        echo "Defined.";
        else
        echo "Not defined.";
        endif
        g++ -Wall -pedantic -g helloWorld.cpp -o h 

当我在命令行中使用make foo=bar调用时,出现以下错误:

bar
"not set"
echo "Argument entered is bar"
Argument entered is bar
ifdef foo
make: ifdef: Command not found
make: *** [helloWorld] Error 127

我已经查看了一些关于这个错误的SO链接,但是还没有解决这个问题。


2
#ifdef 是一个 make 构造,你需要像 shell 命令一样缩进。不要这样做。移除前导制表符。 - Etan Reisner
2
为什么打上了C++的标签?这里并没有涉及到C++。 - abelenky
请参考GNU make手册中的条件示例,了解如何准确地执行此类操作。 - Etan Reisner
@EtanReisner:那是一个回答,不是评论。 - Lightness Races in Orbit
1个回答

13

你的语法有误。你把编译指令 (ifdef, elseendif) 缩进得像 shell 命令一样。

参见 GNU make 手册中条件示例,了解如何做这种事情。

libs_for_gcc = -lgnu
normal_libs =

foo: $(objects)
ifeq ($(CC),gcc)
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接