Using-MM to generate include directives and dependent builds (make include directive and dependency generation WITH-MM)

Source: Internet
Author: User

I want a build rule to being triggered by an include directive if the target of the include is out of date or doesn ' t exist.

Currently the makefile looks like this:

Program_name: = Wget++program_h_srcs: = $ (wildcard *.h) Program_cxx_srcs: = $ (wildcard *.cpp) Program_cxx_objs: = ${ PROGRAM_CXX_SRCS:.CPP=.O}PROGRAM_OBJS: = $ (PROGRAM_CXX_OBJS) DEPS = Make.deps.PHONY:all Clean distcleanall: $ (PROGRAM_ NAME) $ (DEPS) $ (program_name): $ (PROGRAM_OBJS)    $ (link.cc) $ (PROGRAM_OBJS)-O $ (program_name) Clean:    @-$ (RM) $ ( program_name)    @-$ (RM) $ (PROGRAM_OBJS)    @-$ (RM) Make.depsdistclean:cleanmake.deps: $ (PROGRAM_CXX_SRCS) $ ( PROGRAM_H_SRCS) $ (    CXX) $ (cppflags)-mm $ (PROGRAM_CXX_SRCS) > Make.depsinclude $ (DEPS)

The problem is, it seems like the include directive are executing before the rule to build make.deps which effectively means that make are either getting no dependency list if make.deps doesn ' t exist or always getting the make.deps from the P Revious build and not the current one.

For example:

$ make clean $ makemakefile:32:make.deps:no such file or directoryg++  -mm addrCache.cpp connCache.cpp httpClient.cpp Wget++.cpp > make.depsg++    -c-o addrcache.o addrcache.cppg++    -c-o conncache.o conncache.cppg++    -c-o htt PCLIENT.O httpclient.cppg++    -c-o wget++.o wget++.cppg++      addrcache.o conncache.o httpclient.o wget++.o-o wget+ +

Edit

I Read the docs for the include directive, and it sounds like if the include target doesn ' t exist it would continue process ing the parent makefile try and build the target, but it's not completely clear to me how this works:

If an included makefile cannot was found in any of the these directories, a warning message is generated, but it's not a imme diately fatal error; Processing of the makefile containing the include continues. Once It has finished reading makefiles, make would try to remake any that is out of date or don ' t exist. See sections how makefiles is remade. Only after it had tried to find a-remake a Makefile and failed, would make diagnose the missing makefile as a fatal Error.

ANSWER

This is a modification of the answer I accepted. The one thing missing was, the dependency files also depend on the sources, and won ' t get regenerated unless they is Added to the deps files which is being included:

%.D: $ (PROGRAM_CXX_SRCS)    @ $ (CXX) $ (cppflags)-mm $*.cpp | sed-e ' [email protected]^\ (. *\) \.o:@\1.d \1.o:@ ' > [EMA] Il protected]

Sed adds the name of the. d file to the beginning of each of the dependency line like so:

FOO.D foo.o:foo.cpp foo.h bar.h baz.h

I got the idea from this amazing paper to the dangers of recursive make:

Recursive make considered harmful

I also add the following to the makefile:

Clean_list + = ${program_srcs:.c=.d}# at the end of the makefile# Include the list of dependancies generated for each OBJEC T file# unless make is called with Target Cleanifneq "$ (makecmdgoals)" "Clean"-include ${program_srcs:.c=.d}endif
Solution Solutions

You is relying on a implicit rule to compile your. cpp files. You have to redefine it for use the-mm AND-MF flags that would create the dependency file.

%.O:%.cpp    $ (CXX) $ (cppflags) $ (cxxflags)-C $<-o [email protected]-MM-MF [email protected]

Then, you have to include these dependencies files in the Makefile, using-include that won't error when the Dependenci Es files don't exist yet (on the first time, or after a clean).

Program_deps: = $ (PROGRAM_OBJS:.O=.O.D)-include $ (program_deps)

And remember to add the RM command for the dependencies files in the clean rule.

This address: It house»make include directive and dependency generation WITH-MM

Problem

If the target of the include has expired or does not exist, I want a construction rule to be triggered by the include directive.



Currently makefile like this:



  
Program_h_srcs:= $ (wildcard *. H)
Program_cxx_srcs: = $ (wildcard *. cpp)

Program_objs:= $ (PROGRAM_CXX_OBJS)





all:$ (program_name) $ (DEPS)

$ (program_name): $ (PROGRAM_OBJS)
$ (link.cc) $ (PROGRAM_OBJS)-O $ (program_name)

Clean
@ - $ (RM) $ (program_name)
@ - $ (PROGRAM_OBJS)
@ -



make.deps:$ (PROGRAM_CXX_SRCS) $ (PROGRAM_H_SRCS)


Include $ (DEPS)


The problem is that the include directive executes make.deps before the rule, which effectively means make or no dependency list if Make.deps does not exist or is always getting make.deps from the previous version, not the current version



For example:



  

Makefile:32:make.deps: No such file or directory








Edit



I read the documentation for the include directive, it sounds like the inclusion target does not exist it will continue to process the parent makefile try and build the target, but it is not entirely clear how I work:




If the contained makefile cannot be
In any of these directories, generate a
Warning message, but it
Not an immediate and fatal error;
Process contains
The makefile contains the continuation. Once it has
Complete reading Makefile,make will
Try to re-create any obsolete
Or does not exist. See How to
Makefile's remade. Only in
Try to find a way to recreate a
Makefile and fails, it will make
Diagnosis of the missing makefile is
Fatal error.



ANSWER



This is the modification of the answer I accept. One thing that is missing is that dependent files are also dependent on the source and are not regenerated unless they are added to the included Deps file:



%d:$ (PROGRAM_CXX_SRCS)



The sed. d file begins at the beginning of each dependency line as follows:



  


This amazing article is a risky idea for recursive make:



Recursive use of harmful



I also added the following in makefile:



  

# at the end of the makefile
# includes dependency list object files generated for each
# Unless you use the target clean call
Ifneq "$ (makecmdgoals)" "Clean"



Solution Solutions

You are relying on an implicit rule to compile a. cpp file. You must redefine it to use the-MM and-MF flags that will create the dependent files.







Then, you must include these dependent files in makefile, using-include, when the dependent file does not exist (after the first or cleared), it does not error.



  Program_deps:= $ (program_objs:.o =. OD)
-include $ (program_deps)


And remember to add the RM command for the dependent file in the cleanup rule.


Using-MM to generate include directives and dependent builds (make include directive and dependency generation WITH-MM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.