Re: Per-target make rules.



On May 29, 11:02 am, Thomas Maier-Komor <tho...@xxxxxxxxxxxxxx> wrote:
You could write the object files to different directories, each
directory containing object files with a common set of CFLAGS. Then
rules as below:

$(DDIR)/%.o: %.c
$(CC) $(D_CFLAGS) -c $< -o $@

$(CDIR)/%.o: %.c
$(CC) $(C_CFLAGS) -c $< -o $@

Thanks! So I took your suggestion and did it that way, but then I was
having other issues related to having a bunch of subdirectories for
different .o files. I found something called "static rules" in
Makefiles that accomplish something similar, and while they do prevent
me from using the same source file in both lib and test program
(unlike yours, which allows for that no problems), I'm OK with that.
So I am now doing something like this:

$(LIB_OBJS): %.o: %.cpp
$(CC) $(LIB_CFLAGS) -c $<

$(TEST_OBJS): %.o: %.cpp
$(CC) $(TEST_CFLAGS) -c $<

And that almost works perfect, except now I am having a different
problem. I didn't mention this, but some of the libraries have special
cases per source file; for example, in addition to the above generic
rules, an individual library's Makefile needs to be able to specify
extra options per-file, which I did like this when I was using .cpp.o
before:

special.o: special.cpp
$(CC) $(LIB_CFLAGS) -funroll-loops -Wno-long-long $<

And that would appear in the individual Makefiles that included the
common one. Also, I generate dependency information (using g++ -M) so
I have a separate dependency file with lines in it like:

special.o: special.h include.h something.h

That all used to work fine, but now when I'm using the static rules,
Make complains about overriding existing rules (whereas with implicit
rules, rules for the same target were all combined, with static rules,
they aren't combined). Is there some way I can do this?

I could go back to using the solution of placing .o files in separate
directories... in fact, this is all becoming kind of a nightmare and I
think I may take your suggestion one step further and just put the
tester program source in separate directories altogether.

What a pain. I've spent an unacceptable amount of time today working
on Makefiles and organizing directory structures. I can't come up with
a good solution that I'm happy with.

Thanks,
Jason
.



Relevant Pages

  • Re: Per-target make rules.
    ... directory containing object files with a common set of CFLAGS. ... Makefiles that accomplish something similar, ... I could go back to using the solution of placing .o files in separate ... Of course, when having object files for different architectures, the separation into different directories really makes sense. ...
    (comp.unix.programmer)
  • Re: [RFC] Extending kbuild syntax
    ... The second is the more controversial suggestion. ... The pattern varies over this theme. ... To better express how to use it I have tried to update a few Makefiles ...
    (Linux-Kernel)
  • Re: RFC: Big Makefile patch for WARNS settings
    ... I would like you to give me your thoughts on the attached patch. ... for all top-level Makefiles and override that on a subdir basis. ... work done by the DragonflyBSD folks which I intend to port peu a peu. ... I will separate those changes out and work some more on them on another ...
    (freebsd-hackers)