On Thu, 2026-04-30 at 22:30 +0000, Heime wrote:
>
> I have a problem understanding the utility of order-only dependencies
> in make.
>
> For example, consider
>
> obj/main.o: main.c | obj/
> gcc -c main.c -o obj/main.o
>
> obj/:
> mkdir -p obj
>
> obj/ is built before obj/main.o, but updates to obj/ (e.g., adding
> files) won't trigger a rebuild of obj/main.o.
>
> Why would one add files if the program will not be rebuilt? I think
> that if files are added to a build directory, they are included
> becausethey are needed for updating the program.
In your example here you're right, there's no point. But virtually no
one has a makefile like this. What most people have is a makefile that
needs to compile _more than one_ target. For example:
obj/main.o: main.c | obj/
gcc -c main.c -o obj/main.o
obj/foo.o: foo.c | obj/
gcc -c foo.c -o obj/foo.o
obj/:
mkdir -p obj
Now if you DON'T use order-only prerequisites then when you edit one
source file, some random number of other object files from zero to all
of them will also be rebuilt, because when the outdate object file is
rebuilt the modification time on the obj/ directory changes and any
target considered after that, will be out of date and rebuilt.
--
Paul D. Smith <[email protected]> Find some GNU Make tips at:
https://www.gnu.org http://make.mad-scientist.net
"Please remain calm...I may be mad, but I am a professional." --Mad
Scientist