How To Write A Makefile

Q1) How to write makefile for bulk processing?

I have some files in a folder source. I want to process them using program program and output them into folder target just by typing 

$ make

How should I write a makefile for this? 

Directory tree:

   Makefile 

   program

   /source

      foo.x

      bar.x

      spam.x

   /target

      foo.y

      bar.y

      spam.y

make 
 

A1) Something like this:

SOURCES := $(wildcard source/*)

TARGETS := $(patsubst source/%.x, target/%.y, $(SOURCES))

all: $(TARGETS)

target/%.y: source/%.x

program -i $< -o $@
 

Q2) I have three source files and I want to write a makefile to run them as a program. But it seems that my makefile is not good enough to run. When I typed $ make -f Makefile in the command line, I got such kind of output: 

make: *** No rule to make target `main.c', needed by `main.o'. Stop. 
 

A2) You makefile might be OK. It is complainin that it can't find a source file "main.c" and has no rule to make it. 

Well, obviously, if that's your source, it should exist already - it does not need to be made. You do that with an editor 

So why can't make see your source. Wrong directory, or wrong filename, maybe. 

Or

Make sure you write like this. 

CCC=g++ 

CCFLAGS=-c -D _ANSI_C_SOURCE -D CM_DISCLOSURE -qstaticinline 

EXECUTABLE=execVal 

objects = cmrins.o: \ 

cmotrmcd.o 

$(EXECUTABLE): $(objects) 

$(CCC) $(CCFLAGS) -Wall -o $(EXECUTABLE) $(Objects) $(LDFLAGS) 

cmrins.o: 

$(CCC) $(CCFLAGS) cmrins.cpp 

cmotrmcd.o: 

$(CCC) $(CCFLAGS) cmotrmcd.cpp 
 

After you write this also, if the error comes, you do the "touch main.c" 

That will change the timestamp of the main.c 

and use 

make -f MakefileName 

This will make forced execution. 

Unix Tips

See Also
Change Last Modified Accessed Time

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.