0

Problem

I am trying to use if and else statement in a Makefile but all the things I tried down below are not working.

What I tried

The command that I putted on the terminal was:

MinGW32-make test

These variables are at the top of the Makefile:

true = 1
false = 0

ifneq statement:

test:
    ifneq ($(true), $(false))
        echo "1";
    
    else
        echo "2";
    endif

Bash if:

test:
    if [[ "a12" = "a12" ]]; then
        echo "12";
    fi

Important Details

OS : Windows
Text Editor : VScode
I am using mingw64
Meniev
  • 148
  • 1
  • 13
  • Does this answer your question? [If conditions in a Makefile, inside a target](https://stackoverflow.com/questions/15977796/if-conditions-in-a-makefile-inside-a-target) – Brian61354270 Jul 31 '23 at 20:36
  • Please focus on one issue at a time. I believe `ifneq` must not be indented. Not sure about the second error, perhaps you used spaces instead of tabs. I also think you need ``\`` at the end of every line of the `if` except the last one, but forgetting those would give a different error message. – HolyBlackCat Jul 31 '23 at 20:36
  • @Brian61354270 I already see this question, and I think the person is on Linux because he wrote /usr/include/asm-i386/unistd.h – Meniev Jul 31 '23 at 20:39
  • @HolyBlackCat No, I show what I tried. But what I am looking for is a way to use if and else statement in a Makefile. – Meniev Jul 31 '23 at 20:40
  • Just because the example he used was on Linux, doesn't mean the issues he saw were unique to Linux. GNU Make is portable and the structure of makefiles is the same between Linux and Windows. The details, of course, differ widely. – MadScientist Jul 31 '23 at 20:42

1 Answers1

0

ifeq, ifneq, etc. are makefile operations, not shell operations. That means that they cannot be indented by a TAB character in a recipe, because everything indented by a TAB character in a recipe is assumed to be a shell command and is passed to the shell.

If you write this:

test:
ifneq ($(true), $(false))
        echo "1"
else
        echo "2"
endif

then it will work, although that may not be what you want to do (your example seems so simple and far-removed from what you really want to do, that likely any of our advice will not be that helpful--when coming up with examples it's best if it is as close to what you really want to do as possible, while still being simple).

The reason your shell script examples fail is that every logical line in a recipe is sent to a different shell. So when you write:

test:
        if [[ "a12" = "a12" ]]; then
            echo "12";
        fi

make first invokes a shell with just the first line: if [[ "a12" = "a12" ]]; then. That is not a valid shell script so you get an error. If you want to send multiple physical lines into one shell script you have to use backslashes to continue them into a single logical line:

test:
        if [[ "a12" = "a12" ]]; then \
            echo "12"; \
        fi
MadScientist
  • 92,819
  • 9
  • 109
  • 136