Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
Christian Neverdal Avatar asked Feb 12 '26 09:02

Christian Neverdal


1 Answers

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.

like image 83
Kevin Avatar answered Feb 14 '26 19:02

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!