Friday, November 9, 2012

How to compile only the last modified C file in the directory using Makefile?

Assume work is the current directory. Assume there are p1.c, p2.c, p3.c files are there. Each .c file has main function. I want to compile only the last modified file using Makefile. Is it possible ?

Yes.

Write a Makefile[ vim Makefile in linux terminal] and type below commands.


SRC=$(shell ls -rt | tail -1)
test: $(SRC)
        gcc -o test $(shell ls -rt | tail -1)

Save the Makefile using [escape :wq!]

Execute using make command.
./test will give the output of last modified file. If I have modified p2.c it will show output of p2.c.

No comments:

Post a Comment