hugomg
hugomg

Reputation: 69964

Is there a way to tell my makefile to output a custom error message if a certain an include file is not found?

I have a configure script that generates a config.inc file containing some variable definitions and a makefile that imports those configurations using

include config.inc

The thing that bothers me is that if the user tries to run the makefile directly without first running configure they get an unhelpful error message:

makefile:2: config.inc: No such file or directory
make: *** No rule to make target 'config.inc'.  Stop.

Is there a way for me to produce a better error message, instructing the user to first run the configure script, without resorting to the autoconf strategy of generating the full makefile from inside configure?

Upvotes: 2

Views: 2718

Answers (2)

hugomg
hugomg

Reputation: 69964

I gave up and decided to use autoconf and automake to handle my makefile-generation needs.

Upvotes: 0

MadScientist
MadScientist

Reputation: 101081

Sure, no problem; just do something like this:

atarget:
        echo here is a target

ifeq ($(wildcard config.inc),)
  $(error Please run configure first!)
endif

another:
        echo here is another target

include config.inc

final:
        echo here is a final target

Note this is definitely specific to GNU make; there's no portable way to do this.

EDIT: the above example will work fine. If the file config.inc exists, then it will be included. If the file config.inc does not exist, then make will exit as it reads the makefile (as a result of the error function) and never get to the include line so there will be no obscure error about missing include files. That's what the original poster asked for.

EDIT2: Here's an example run:

$ cat Makefile
all:
        @echo hello world

ifeq ($(wildcard config.inc),)
  $(error Please run configure first!)
endif

include config.inc

$ touch config.inc

$ make
hello world

$ rm config.inc

$ make
Makefile:5: *** Please run configure first!.  Stop.

Upvotes: 5

Related Questions