Christian Neverdal
Christian Neverdal

Reputation: 5375

Keep printing compiler warnings from compiled code using g++ and Makefiles

I have a Makefile that only compiles/recompiles what's necessary. However, even after something is compiled I would like to see their compile warnings.

My scenario is this: 1) I make a file called snake.cpp. 2) I make my project. snake.cpp has 3 warnings and 0 errors. 3) I make another file called cup.cpp 4) I make my project, now I see cup.cpp's warnings but none of snake.cpp's warnings.

But I want to see snake.cpp's warnings, even though the compiling is skipped by make. I don't want to recompile the whole project with -Werror. In fact, I don't want to recompile anything unless I actually edited the relevant files, but I would still like to see the warnings.

What is the best way of achieving this?

Upvotes: 3

Views: 342

Answers (1)

Kevin
Kevin

Reputation: 56059

The compiling isn't skipped "by mistake", it's skipped because the executable/object is newer than the source. That's how makefiles work. And the only way to get the warnings without recompiling is by storing them somewhere. It sounds like your solution is to redirect errors to a log file and print them back out when you make. Perhaps this will work, but I haven't tested it:

all: snake cup
    @cat snake.err
    @cat cup.err

snake: snake.cpp
    g++ snake.cpp -o snake 2>snake.err

cup: cup.cpp
    g++ cup.cpp -o cup 2>cup.err

.PHONY: all

If they're both part of the same executable you'll want to make objects instead.

Upvotes: 3

Related Questions