Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:06:44+00:00 2026-06-05T09:06:44+00:00

How could I abort a make/makefile execution based on a makefile’s variable not being

  • 0

How could I abort a make/makefile execution based on a makefile’s variable not being set/valued?

I came up with this, but works only if caller doesn’t explicitly run a target (i.e. runs make only).

ifeq ($(MY_FLAG),)
abort:   ## This MUST be the first target :( ugly
    @echo Variable MY_FLAG not set && false
endif

all:
    @echo MY_FLAG=$(MY_FLAG)

I think something like this would be a good idea, but didn’t find anything in make’s manual:

ifndef MY_FLAG
.ABORT
endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:06:46+00:00Added an answer on June 5, 2026 at 9:06 am

    TL;DR: Use the error function:

    ifndef MY_FLAG
    $(error MY_FLAG is not set)
    endif
    

    Note that the lines must not be indented. More precisely, no tabs must precede these lines.


    Generic solution

    In case you’re going to test many variables, it’s worth defining an auxiliary function for that:

    # Check that given variables are set and all have non-empty values,
    # die with an error otherwise.
    #
    # Params:
    #   1. Variable name(s) to test.
    #   2. (optional) Error message to print.
    check_defined = \
        $(strip $(foreach 1,$1, \
            $(call __check_defined,$1,$(strip $(value 2)))))
    __check_defined = \
        $(if $(value $1),, \
          $(error Undefined $1$(if $2, ($2))))
    

    And here is how to use it:

    $(call check_defined, MY_FLAG)
    
    $(call check_defined, OUT_DIR, build directory)
    $(call check_defined, BIN_DIR, where to put binary artifacts)
    $(call check_defined, \
                LIB_INCLUDE_DIR \
                LIB_SOURCE_DIR, \
            library path)
    

    This would output an error like this:

    Makefile:17: *** Undefined OUT_DIR (build directory).  Stop.
    

    Notes:

    The real check is done here:

    $(if $(value $1),,$(error ...))
    

    This reflects the behavior of the ifndef conditional, so that a variable defined to an empty value is also considered “undefined”. But this is only true for simple variables and explicitly empty recursive variables:

    # ifndef and check_defined consider these UNDEFINED:
    explicitly_empty =
    simple_empty := $(explicitly_empty)
    
    # ifndef and check_defined consider it OK (defined):
    recursive_empty = $(explicitly_empty)
    

    As suggested by @VictorSergienko in the comments, a slightly different behavior may be desired:

    $(if $(value $1) tests if the value is non-empty. It’s sometimes OK if the variable is defined with an empty value. I’d use $(if $(filter undefined,$(origin $1)) ...

    And:

    Moreover, if it’s a directory and it must exist when the check is run, I’d use $(if $(wildcard $1)). But would be another function.

    Target-specific check

    It is also possible to extend the solution so that one can require a variable only if a certain target is invoked.

    $(call check_defined, ...) from inside the recipe

    Just move the check into the recipe:

    foo :
        @:$(call check_defined, BAR, baz value)
    

    The leading @ sign turns off command echoing and : is the actual command, a shell no-op stub.

    Showing target name

    The check_defined function can be improved to also output the target name (provided through the $@ variable):

    check_defined = \
        $(strip $(foreach 1,$1, \
            $(call __check_defined,$1,$(strip $(value 2)))))
    __check_defined = \
        $(if $(value $1),, \
            $(error Undefined $1$(if $2, ($2))$(if $(value @), \
                    required by target `$@')))
    

    So that, now a failed check produces a nicely formatted output:

    Makefile:7: *** Undefined BAR (baz value) required by target `foo'.  Stop.
    

    check-defined-MY_FLAG special target

    Personally I would use the simple and straightforward solution above. However, for example, this answer suggests using a special target to perform the actual check. One could try to generalize that and define the target as an implicit pattern rule:

    # Check that a variable specified through the stem is defined and has
    # a non-empty value, die with an error otherwise.
    #
    #   %: The name of the variable to test.
    #   
    check-defined-% : __check_defined_FORCE
        @:$(call check_defined, $*, target-specific)
    
    # Since pattern rules can't be listed as prerequisites of .PHONY,
    # we use the old-school and hackish FORCE workaround.
    # You could go without this, but otherwise a check can be missed
    # in case a file named like `check-defined-...` exists in the root 
    # directory, e.g. left by an accidental `make -t` invocation.
    .PHONY : __check_defined_FORCE
    __check_defined_FORCE :
    

    Usage:

    foo :|check-defined-BAR
    

    Notice that the check-defined-BAR is listed as the order-only (|...) prerequisite.

    Pros:

    • (arguably) a more clean syntax

    Cons:

    • One can’t specify a custom error message
    • Running make -t (see Instead of Executing Recipes) will pollute your root directory with lots of check-defined-... files. This is a sad drawback of the fact that pattern rules can’t be declared .PHONY.

    I believe, these limitations can be overcome using some eval magic and secondary expansion hacks, although I’m not sure it’s worth it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I could just make all the Mix_Musics public, and not worry about
I've been working on a makefile that uses secondary expansion not knowing that this
In a GNU makefile, I would like to set an output variable to one
Ok guys, I started to learn more about Jquery and now I could make
I've read all the posts I could find about this, and none helped. I've
The location text from Twitter could be just about anything. Sometimes Twitter clients set
when a System.Web.HttpResponse.End() is called a System.Thread.Abort is being fired, which i'm guessing is
Ok, it's not about compiling exactly, but recently I discovered the :compiler command in
I am installing a library, and got this error message: xxxx@ubuntu$ make (cd num;
I asked this question before but I'm going to complete the question with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.