Makefile -- error out if variable not set

nathanleclaire

Following advice at Abort makefile if variable not set I am attempting to use ifndef but it's not working.

Makefile:

foo:
        ifndef BAR
        $(error "Must set BAR")
        endif

But make foo BAR=quux does not work:

Makfile:2: *** "Must set BAR". Stop.

What gives? Is it whitespace issue? Or am I misunderstanding completely?

(I'm using GNU Make)

I tried also:

foo:
ifndef BAR
$(error "Must set BAR")
endif
        echo $(BAR)

Which seems to sort of work but still causes the ifndef to be invoked if a target further down in the Makefile (which does not require the variable to be set) is invoked.

Beta

Technically, yes, it's a whitespace problem, but more fundamentally it's a scope problem.

The ifndef... endif is a Make conditional. So Make will evaluate it before executing any rule. And since the ifndef is not part of a recipe, it should not be preceded by a TAB. This works:

ifndef BAR
$(error "Must set BAR") # this is a Make error
endif

foo:
ifndef BAR
    echo "Must set BAR" # this is a shell command
    exit 1              # this is another shell command
endif

But what you did was this:

foo:
    ifndef BAR
    $(error "Must set BAR")
    endif

You put a TAB in front of the three lines (ifndef..., $(error... and endif), so Make assumed that those were commands, to be passed to the shell if and when the foo recipe was executed. And when Make executed the rule, it expanded the Make-syntax statement $(error...) -- and terminated with an error -- before passing anything to the shell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefile set variable in target

From Dev

Check if environmental variable is set in makefile

From Java

How to set child process' environment variable in Makefile

From Dev

Makefile: read input variable and set several variables

From Dev

Is it possible to set environment variable through a makefile?

From Dev

How to conditionally set Makefile variable to something if it is empty?

From Dev

Rule to set a variable in Makefile not working as expected

From Dev

How to conditionally set Makefile variable to something if it is empty?

From Dev

Object variable not set error

From Dev

How to set a hotkey to save and run makefile and ./a.out in vim?

From Dev

Why is a commented out $(error ...) in my Makefile getting triggered?

From Dev

JSLint: used out of scope for ternary variable set

From Dev

Variable of object set in construct is out of scope in destruct

From Dev

Makefile: Default Value of Variable that is set but has null value

From Dev

Syntax Error With Set Variable In Batch

From Dev

Object variable or With block variable not set (Error 91)

From Dev

Batch - set variable error; variable(%)(string)(%%a)(%)?

From Dev

Set static variable to ssis variable - error

From Dev

Object variable or With block variable not set error in vba

From Dev

python syntax error can figure out!! variable

From Dev

rvest error: "Error in class(out) <- "XMLNodeSet" : attempt to set an attribute on NULL"

From Dev

PHP $this when not in object context for set public variable from out of class

From Dev

C# Set environment variable in operative system windows (out of execution)

From Dev

Which is faster: Assigning same value to a variable, or finding out if it is already set?

From Dev

How to find out the variable names for debconf-set-selections?

From Dev

Error in Qt Creator: "The environment variable ExtensionSdkDir is not set"

From Dev

VBA Error 91: Object Variable not set

From Dev

Batch error not letting me set a variable

From Dev

PHP SQL Syntax Error set variable

Related Related

  1. 1

    Makefile set variable in target

  2. 2

    Check if environmental variable is set in makefile

  3. 3

    How to set child process' environment variable in Makefile

  4. 4

    Makefile: read input variable and set several variables

  5. 5

    Is it possible to set environment variable through a makefile?

  6. 6

    How to conditionally set Makefile variable to something if it is empty?

  7. 7

    Rule to set a variable in Makefile not working as expected

  8. 8

    How to conditionally set Makefile variable to something if it is empty?

  9. 9

    Object variable not set error

  10. 10

    How to set a hotkey to save and run makefile and ./a.out in vim?

  11. 11

    Why is a commented out $(error ...) in my Makefile getting triggered?

  12. 12

    JSLint: used out of scope for ternary variable set

  13. 13

    Variable of object set in construct is out of scope in destruct

  14. 14

    Makefile: Default Value of Variable that is set but has null value

  15. 15

    Syntax Error With Set Variable In Batch

  16. 16

    Object variable or With block variable not set (Error 91)

  17. 17

    Batch - set variable error; variable(%)(string)(%%a)(%)?

  18. 18

    Set static variable to ssis variable - error

  19. 19

    Object variable or With block variable not set error in vba

  20. 20

    python syntax error can figure out!! variable

  21. 21

    rvest error: "Error in class(out) <- "XMLNodeSet" : attempt to set an attribute on NULL"

  22. 22

    PHP $this when not in object context for set public variable from out of class

  23. 23

    C# Set environment variable in operative system windows (out of execution)

  24. 24

    Which is faster: Assigning same value to a variable, or finding out if it is already set?

  25. 25

    How to find out the variable names for debconf-set-selections?

  26. 26

    Error in Qt Creator: "The environment variable ExtensionSdkDir is not set"

  27. 27

    VBA Error 91: Object Variable not set

  28. 28

    Batch error not letting me set a variable

  29. 29

    PHP SQL Syntax Error set variable

HotTag

Archive