Re: Paths above current/working directory don't work with suffix rules

2022-01-24 Thread Adam Tuja
Thanks for an answer. With your help I was able to come up with this piece:
--PROG = ab
LD = $(CC)
SRCS = a.c b.c ../c-1up.c
OBJS = $(SRCS:.c=.o)
.c.o:        $(CC) $(CFLAGS) -c -o $@ $<
.cpp.o:        $(CXX) $(CXXFLAGS) -c -o $@ $<
all: $(PROG)
$(PROG): $(OBJS)
$(LD) -o $@ $(OBJS) $(LIBS)
--Now, when I understand it, it works. It's not the same thing as objects are placed beside source files but step forward nevertheless.I will check VPATH later.
Regards
Adam




Re: Paths above current/working directory don't work with suffix rules

2022-01-24 Thread Paul Smith
On Mon, 2022-01-24 at 21:27 +0100, Adam Tuja wrote:
> SRCS = a.c b.c ../c-1up.c
> OBJS = $(SRCS:.c=.o)
> 
> .c.o:
> $(CC) $(CFLAGS) -c -o $@ $<
> 
> $(PROG): $(OBJS)
> 
> Now, when I understand it, it works. It's not the same thing as
> objects are placed beside source files but step forward nevertheless.

Did you mean to write something different in your last sentence?  It 
IS the same thing as objects placed beside source files, unless I
misunderstand what you mean by "beside".

This makefile will always write the object files into the same
directory as the source files.  As long as you want to do that, then
you can use suffix rules or pattern rules very easily.

It's when you want object files to go into a different directory from
source files that suffix rules won't work well (you can use VPATH to
help somewhat).




Re: Paths above current/working directory don't work with suffix rules

2022-01-24 Thread Paul Smith
On Mon, 2022-01-24 at 19:09 +0100, Adam Tuja wrote:
> It doesn't matter how with many levels above current directory the
> path has (../ ../../ ...), it's still the same.

Suffix rules are very limited: this is why pattern rules were invented.

As long as you're happy with creating the object file in the current
directory, AND you're happy with ensuring that no two directories ever
have the same source file name, then it's possible to use suffix rules
to build them.

However you can see from the rule:

  .c.o:

that there's nothing here to tell make where the source file will be,
so clearly that cannot work by itself.  A suffix rule is identical to
the pattern rule:

  %.o : %.c

so, the prefix of the target and prerequisite MUST be identical
strings.  The string "c-1up" is not identical to the string "../c-1up", 
and so this rule cannot match.

You can use VPATH to give make a list of directories to use to search
for source files.  So, in your makefile if you add this:

  VPATH = .. ../..

when make wants to build c-1up.c it will look for ./c-1up.c, then
../c-1up.c, then ../../c-1up.c, and build the first one it finds.

https://www.gnu.org/software/make/manual/html_node/Directory-Search.html

> PS.
> Not to mention that if I don't specify target (all) then it only
> processes one file

Make has to choose SOME target to build by default, if you don't
specify one.  Make always chooses the first target in the makefile by
default.  So, move your "all" target to the top so it's the first
target and it will work as you expect.

https://www.gnu.org/software/make/manual/html_node/Goals.html




Paths above current/working directory don't work with suffix rules

2022-01-24 Thread Adam Tuja
Hello,I had this problem trying to compile a source with relative paths above current/working directory level. I took me hours of frustration to make work and, it eventually didn't.Then, by a chance I realised that when I use a path within current working directory it more or less works.It doesn't matter how with many levels above current directory the path has (../ ../../ ...), it's still the same.--PROG = abLD = $(CC)OBJS = a.o b.o c-1up.oa.o: a.cb.o: b.cc-1up.o: ../c-1up.c.c.o:        $(CC) $(CFLAGS) -c -o $@ $<.cpp.o:        $(CXX) $(CXXFLAGS) -c -o $@ $<$(PROG): $(OBJS)        $(LD) -o $@ $(OBJS) $(LIBS)all: $(PROG)--```$ make -f makefile-ab3x-suffix-rules.mk allcc  -c -o a.o a.ccc  -c -o b.o b.ccc -o ab a.o b.o c-1up.o cc: error: c-1up.o: No such file or directorymake: *** [ab] Error 1```And then I found that it only fails when I use suffix rules. When every file has its own rule it works.--PROG = abLD = $(CC)OBJS = a.o b.o c-1up.oa.o: a.c        $(CC) $(CFLAGS) -c -o $@ $