Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-08-2005, 08:05 PM   #1
Registered: May 2004
Location: uk
Distribution: debian sarge
Posts: 65

Rep: Reputation: 15
make, subdirs, and patsubst

I am trying to use pattern substitution and wildcards to modifiy a Makefile which will compile all src/*.cc files into src/*.o files and then link into the main program.

Unfortunately, I can only find two unsatisfactory sollutions. The first places the object files in the main directory (ie not in src) and then can't find them when it comes to linking. The second places the object files in src, but disregards $(CFLAGS).

Here is the relevant portion of my Makefile:

SRC := $(wildcard src/*.cc)
OBJS := $(
HEADERS := $(wildcard include/*.h)

$(PROG): $(OBJS)
# @echo OBJS are $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(PROG)

%.o : $(HEADERS)
# @echo OBJS are $(OBJS)
$(CC) $(CFLAGS) -c $<

main.o: main.c $(HEADERS)

rm -f $(PROG) $(OBJS)

BTW: This is the version which places the objs in the main dir. if I change %.o : %.c $(HEADERS) to src/%.o : %.c $(HEADERS) then the other result occurs.

The commented out echos show that atleast the pattern substitution works and that the src/ prefix is there.

I've no clue as to why then that the object files are not placed where I want them to be.

Old 04-09-2005, 09:41 AM   #2
Registered: May 2004
Location: uk
Distribution: debian sarge
Posts: 65

Original Poster
Rep: Reputation: 15
The solution was to change
$(CC) $(CFLAGS) -c $<

$(CC) $(CFLAGS) -c $< -o $@

I had been confused by the $< thingy, thinking it was magic of some kind. But it's just a variable with the first pre-requisite, ie the each of the source files, and once I knew that, there had to be an equivalant variable for the target...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
disapearing and unaccessible subdirs NonSumPisces Linux - General 2 05-29-2004 11:51 AM
recurse through subdirs? Axion Linux - Software 4 02-16-2004 08:12 PM
mounting subdirs dARkHunTEr Linux - General 2 11-04-2003 08:32 AM
What does compiler process "$(patsubst %, _mod_%, $(SUBDIRS))"? isaac huagn Programming 3 07-30-2003 03:52 AM
How would you cp a file to multi subdirs? m0rl0ck Linux - General 2 03-04-2003 12:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration