[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The order of compiling multiple c++ source files

From: ljh
Subject: Re: The order of compiling multiple c++ source files
Date: Wed, 10 Aug 2022 12:39:57 +0800

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


    ` 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



# 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" <psmith@gnu.org>;
Date: Wed, Aug 10, 2022 06:17 AM
To: "ljh"<ljhm@qq.com>;"bug-make"<bug-make@gnu.org>;
Subject: Re: The order of compiling multiple c++ source files

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

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

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

  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.  You'll note
in your output that the link line is:

  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"?  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

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

[Prev in Thread] Current Thread [Next in Thread]