Daniel Dilts wrote:
I believe that the second rule will be run for each file. The first
implied
rule will be overwritten by the second. Therefore, every .cpp file will be
treated as depending on every .h file.
Someone please correct me if I wrong.
This works just fine for me:
Makefile:
cfiles:=$(wildcard src/*.c)
ofiles:=$(patsubst src/%.c,obj/%.o,$(cfiles))
all: $(ofiles)
obj/%.o: src/%.c src/%.h
@echo gcc $< -o $@
@touch $@
obj/%.o: src/%.c
@echo gcc $< -o $@
@touch $@
[EMAIL PROTECTED] makefiles]$ touch src/a.c
[EMAIL PROTECTED] makefiles]$ make
gcc src/a.c -o obj/a.o
[EMAIL PROTECTED] makefiles]$ touch src/b.c src/b.h
[EMAIL PROTECTED] makefiles]$ make
gcc src/b.c -o obj/b.o
[EMAIL PROTECTED] makefiles]$ touch src/b.h
[EMAIL PROTECTED] makefiles]$ make
gcc src/b.c -o obj/b.o
[EMAIL PROTECTED] makefiles]$ touch src/c.c
[EMAIL PROTECTED] makefiles]$ make
gcc src/c.c -o obj/c.o
[EMAIL PROTECTED] makefiles]$ touch src/c.h
[EMAIL PROTECTED] makefiles]$ make
gcc src/c.c -o obj/c.o
Frank
On 10/11/06, Frank Sorenson <[EMAIL PROTECTED]> wrote:
Daniel Dilts wrote:
> One more problem occured to me with the rule I made:
>
> $(OBJECT_PATH)/%.o : $(SOURCE_PATH)/%.cpp
> $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
>
> This does not make any of the header files a dependency. Any ideas
for how
> to add that without hardcoding each of the heading file names (and only
> have
> as dependencies the header files that are actually used)?
How about:
$(OBJECT_PATH)/%.o : $(SOURCE_PATH)/%.cpp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
$(OBJECT_PATH)/%.o: $(SOURCE_PATH)/%.cpp $(SOURCE_PATH)/%.h
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
If a .h file exists, the second rule will apply, otherwise, the original
rule will build the object.
Frank
>
> On 10/6/06, Frank Sorenson <[EMAIL PROTECTED]> wrote:
>>
>> Daniel Dilts wrote:
>> > Given a directory ./src that has an arbitrary number of .cpp files,
and
>> a
>> > directory ./obj that will contain the object files, I am trying to
>> create a
>> > Makefile that will automatically create all of the .o files,
regardless
>> of
>> > what files are added/removed from ./src.
>> >
>> > My thought is to use an included Makefile that has the rules to
create
>> each
>> > of the .o files.
>> >
>> > To create the .o files I use g++ -MM src/*cpp > Makefile.obj to
create
>> the
>> > included file.
>> >
>> > My Makefile looks like this:
>> >
>> > target : ...
>> >
>> > Makefile.obj : dummy
>> > g++ -MM src/*cpp > Makefile.obj
>> >
>> > .PHONY : dummy
>> > dummy :
>> > -@ ls > /dev/null 2> /dev/null
>> >
>> > include Makefile.obj
>> >
>> >
>> >
>> > I'm trying to make sure that Makefile.obj is recreated each time
that
>> make
>> > is run. The dummy target is the only way I can think of making sure
>> that
>> > the Makefile.obj target is always run, but this seems like a really
bad
>> way
>> > of doing this. The problem (not a big deal in this case) is that
the
>> > Makefile.obj target gets run 2-3 times.
>> >
>> > Also, on a sidenote, this creates a file with the format:
>> > file.o: file.cpp ...
>> >
>> > I'm also trying to figure out how to prepend "obj/" to each line in
the
>> > file.
>>
>> I'd suggest using the wildcard patterns to simplify this whole mess.
>> Perhaps something more along the lines of this:
>>
>> Makefile:
>> cfiles:=$(wildcard src/*.cpp)
>> ofiles:=$(patsubst src/%.cpp,obj/%.o,$(cfiles))
>>
>> all: $(ofiles)
>>
>> obj/%.o: src/%.cpp
>> @echo g++ $? -o $@
>> @touch $@
>>
>> Put your files into src, and it'll compile them all for you.
>>
>> Frank
>>
>>
>> --------------------
>> BYU Unix Users Group
>> http://uug.byu.edu/
>>
>> The opinions expressed in this message are the responsibility of their
>> author. They are not endorsed by BYU, the BYU CS Department or
BYU-UUG.
>> ___________________________________________________________________
>> List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
>>
> --------------------
> BYU Unix Users Group http://uug.byu.edu/
> The opinions expressed in this message are the responsibility of their
> author. They are not endorsed by BYU, the BYU CS Department or
BYU-UUG.
> ___________________________________________________________________
> List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
--------------------
BYU Unix Users Group
http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
--------------------
BYU Unix Users Group http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
--------------------
BYU Unix Users Group
http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list