help-make
[Top][All Lists]
Advanced

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

Re: implicit versus explicit rules and compiling all source files with a


From: Paul D. Smith
Subject: Re: implicit versus explicit rules and compiling all source files with automatic variables. some gnumake install info ??
Date: Sun, 20 Oct 2002 22:22:20 -0400

%% Dave <address@hidden> writes:
 
  d> make version is 3.79

You should get at least 3.79.1.  3.79 had bugs that necessitated
releasing 3.79.1 within a month or two.

The current released version is GNU make 3.80.
 
  d> Where I am perplexed with overriding or writing my own rules is the
  d> command for compiling all *.cpp or %.cpp files in my current
  d> directory.  The cpp files do exist for starters. The output object
  d> .o files do not as expected. I expect to place the output files
  d> form just compiling to *.o in a subdirectory under the current
  d> source directory.
 
  d> I want to use automatic variables and as much wildcard and lists as
  d> possible to complete tasks. prerequisites I believe are satisfied with
  d> source files *.cpp
 
  d> MYOBJS := $(patsubst %.cpp, %.o, $(wildcard *.cpp))
 
  d> DLL_TARGET_NAME=$(BIN_DIR)/lib$(DLL_NAME).sl
 
  d> %.o: %.cpp
  d>   $(CPP) $(STD_INCLUDES) $(STD_CPPFLAGS) $(APP_CCFLAGS) $< -o
  d> $(addprefix $(OBJ_DIR), \
  d>               $(addprefix $(TSLASH), $(basename $< ))).o

This is wrong.

You told make that if it wants to build a file "foo.o" and it can find a
file "foo.cpp", then running the command script you provided will update
that "foo.o" based on that "foo.cpp".

But you lied! :)

Your rule does _NOT_ update "foo.o", it updates $(OBJ_DIR)/foo.o.

Make is like your shrink: if you lie to it, it cannot help you.  This is
Paul's Second Rule of Makefiles: your rules should _always_ create a
file "$@".  Don't create "../$@", or "$(OBJDIR)/$@", or anything like
that.  You must create exactly "$@".

You want to do something like this:

  MYOBJS := $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))

  $(OBJ_DIR)/%.o : %.cpp
          $(CPP) $(STD_INCLUDES) $(STD_CPPFLAGS) $(APP_CCFLAGS) $< -o $@

instead.
 
  d> foo: $(MYOBJS)
 
  d> createobjdir:
  d>     - mkdir $(OBJ_DIR)      # Release or Debug

A better way would be to use $(shell ...):

  __x := $(shell mkdir $(OBJ_DIR))


In the future when asking questions it's helpful to us if you choose a
very specific question and provide a small example... emails which have
too many questions at the same time, especially if they're somewhat
vague, are harder for us to read and answer.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




reply via email to

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