Thanks Paul,

I have another quetion. Please help me.


On section 10.2 of the manual:


    ` x: y.o z.o `


Can I put x.o on the prerequisites on the right part? 


    ` x: y.o z.o x.o`  # with x.o


Are they the same?


---


The section 10.2 inspired me to write my little minimal general Makefile for C, 
C++.


The most important one in my Makefile is this single line:


    ` x : $(patsubst %.c,%.o,$(wildcard *.c)) `  # without recipe


or, 


    ` x : $(patsubst %.c,%.o,$(wildcard *.c)) `
    `       $ (CC) $ (LDFLAGS) $^ $(LDLIBS) -o $@ ` 
 # with recipe


The patsubst function part will include x.o. And I can omit the recipe or not.


---


If the code uses C++20 modules, I need to omit x.o if I want to omit the recipe:


    ` x: y.o z.o `  # without x.o and recipe


or I need to write the recipe if I include x.o:


    ` x: y.o z.o x.o`  # with x.o
    `       $ (CXX) $ (LDFLAGS) $^ $(LDLIBS) -o $@ ` 
 # with recipe




Thanks.


---


# My little Minimal Makefile for C, C++
# build shared library with -fPIC, -shared
CFLAGS   = -Wall -Wextra -g # -O3 -fPIC  # CXXFLAGS for .cpp
LDFLAGS  = # -L../hello # -shared
LDLIBS   = # -lhello
CPPFLAGS = -MMD -MP -I../hello
#CC      = $(CXX)  # link with CXX for .cpp


# target name is basename of one of the source files
main : $(patsubst %.c,%.o,$(wildcard *.c))  # .cpp
-include *.d
clean : ; -rm -fr *.o *.d
.PHONY : clean







------------------ Original ------------------
From:                                                                           
                                             "psmith"                           
                                                         <psm...@gnu.org&gt;;
Date:&nbsp;Wed, Aug 10, 2022 06:17 AM
To:&nbsp;"ljh"<l...@qq.com&gt;;"bug-make"<bug-make@gnu.org&gt;;

Subject:&nbsp;Re: The order of compiling multiple c++ source files



On Wed, 2022-08-10 at 04:32 +0800, ljh wrote:
&gt; make manual / 10.2 Catalogue of Built-In Rules / Linking a single
&gt; object file

The example in the manual is wrong.&nbsp; The output you're getting from GNU
make is correct.

The reason is that the implicit rule that make chooses for the rule:

&nbsp; x: y.o z.o

is not "%.o : %.c" followed by "% : %.o".

Instead, make chooses the default rule "% : %.c" which knows how to
create a binary directly from an similarly-named .c file.&nbsp; You'll note
in your output that the link line is:

&nbsp; g++ ... x.cpp y.o z.o a.o d.o -o x

see how you have "x.cpp" on the link line, not "x.o"?&nbsp; And there's no
"x.o" in your directory.

This may not be what you want, but it's a perfectly valid way to build
things.

As for the "rm" that's clearly just wrong: since these objects are
mentioned as prerequisites they cannot be considered intermediate and
won't be deleted.

So, the manual example needs to be fixed.

Reply via email to