Jorge Leitao
Jorge Leitao

Reputation: 20113

How to stop make (in makefile) after "No such file or directory" error?

So, most of the times I'm testing if every include is correct on a given C/C++ code, I have a makefile with a gcc/g++ call with proper -I option for searching headers on specific directories (like every program) when I'm compiling sources to headers.

However, if the included directory is not correct and an undefined header appears (e.g. foo.h has #include and was not found), the gcc/g++ will just spit a bunch of errors for every include I have of that foo.h header for all other sources I'm compiling afterwards (and I'm already using -Werror -Wfatal-errors to make gcc/g++ to stop).

So, my question is simple: how can I tell makefile stop after the first error of the type "No such file or directory" it finds? It is really annoying it continue to compile sources and sources, giving me hundreds of errors just for a repeated error I already understood.

Upvotes: 4

Views: 3304

Answers (3)

Lelanthran
Lelanthran

Reputation: 1529

Put the header files into a variable and use that variable as a dependency. The following snippet will not build anything until the specified headers exist.

HEADERS=test.h other.h /usr/include/special.h

all: $(HEADERS) $(BINPROGS)

[... all other rules go here as usual ...]

*.h:
    echo found $@

The ".h:" simply prints out each header that is found before any building even starts. The makefile itself stops if a header cannot be found (and it will stop before trying to compile anything).

I believe that that is what you wanted?

Upvotes: 3

0xC0000022L
0xC0000022L

Reputation: 21269

It probably continues because you told it to. See the following two options of GNU make:

-k, --keep-going          Keep going when some targets can't be made.
-S, --no-keep-going, --stop
                          Turns off -k.

Upvotes: 3

Aniket Inge
Aniket Inge

Reputation: 25705

you can write a shell script to check for error conditions before running the make script.

Upvotes: 0

Related Questions