'Variables cannot be compared in Makefile

i wrote following code. but i faced the errors.

result=1
ifeq (,1)
/bin/sh: -c: line 1: syntax error near unexpected token `,1'
/bin/sh: -c: line 1: `ifeq (,1)'
test:
     result=$(shell cat test_result | grep "Bad idea" | wc -l)
     ifeq ($(result),1)
          @echo "test passed"
     else
          @echo "test failed"
     endif

Why does this happen? Can you tell me?



Solution 1:[1]

These conditionals need to be at the top-level and are evaluated when the make file is read, not when the target is executed. Further, you'll need something like xargs to strip the whitespace on the result. Then it works like you might expect.

result := $(shell cat test_result | grep "Bad idea" | wc -l | xargs)
ifeq ($(result),1)
    out := "test passed"
else
    out := "test failed"
endif

test:
    echo ${out}

This gives an exchange like:

? make
echo "test failed"
test failed

? echo "Bad idea" > test_result 

? make                         
echo "test passed"
test passed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 theherk