meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:makefile [2016/11/21 21:13]
niziak
programming:makefile [2024/03/22 09:30] (current)
niziak
Line 1: Line 1:
 +====== Makefile ======
 +
 +Quick reference [[https://​www.gnu.org/​software/​make/​manual/​html_node/​Quick-Reference.html]]
 +
 [[http://​aegis.sourceforge.net/​auug97.pdf|Recursive Make Considered Harmfull]] [[http://​aegis.sourceforge.net/​auug97.pdf|Recursive Make Considered Harmfull]]
  
 [[http://​make.mad-scientist.net/​papers/​rules-of-makefiles/​]] [[http://​make.mad-scientist.net/​papers/​rules-of-makefiles/​]]
  
-== VPATH ==+ 
 +=== one shell === 
 + 
 +[[https://​www.gnu.org/​software/​make/​manual/​html_node/​One-Shell.html|5.3.1 Using One Shell]] 
 +<code make> 
 +.ONESHELL:​ 
 +foo : bar/lose 
 +        cd $(<D) 
 +        gobble $(<F) > ../$@ 
 +</​code>​ 
 + 
 +=== disable built-in rules === 
 + 
 +<code makefile>​ 
 +# Disable built-in rules and variables 
 +MAKEFLAGS += --no-builtin-rules 
 +MAKEFLAGS += --no-builtin-variables 
 +</​code>​ 
 + 
 +=== VPATH ==
 [[https://​www.gnu.org/​software/​make/​manual/​html_node/​General-Search.html]] [[https://​www.gnu.org/​software/​make/​manual/​html_node/​General-Search.html]]
  
Line 10: Line 34:
 ''​VPATH = .:​$(OBJDIR):​$(LIBDIR)''​ ''​VPATH = .:​$(OBJDIR):​$(LIBDIR)''​
  
-== order only prereq ==+=== order only prereq ==
 Special pipe "​|"​ character is used.  Special pipe "​|"​ character is used. 
 If any //​$(objects)//​ has to be build, then //obj// has to be build first. If any //​$(objects)//​ has to be build, then //obj// has to be build first.
 But if //obj// is out of date or missing, this doesn'​t force //​$(objects)//​ to built. But if //obj// is out of date or missing, this doesn'​t force //​$(objects)//​ to built.
-<​code>​+ 
 + 
 +<​code ​make>
 $(objects): | obj $(objects): | obj
  
Line 21: Line 48:
 </​code>​ </​code>​
  
 +<code make>
 +targets : normal-prerequisites | order-only-prerequisites
 +</​code>​
 +
 +[[https://​www.gnu.org/​software/​make/​manual/​html_node/​Prerequisite-Types.html]]
 +
 +=== double colon rules ===
 +
 +[[https://​www.chemie.fu-berlin.de/​chemnet/​use/​info/​make/​make_4.html#​SEC40]]
 +
 +It can be useful, when target needs to be updated and command to update target depends on which prerequisite file caused the update.
  
-=== Variables === +<​code ​make
-  * **$@** make target +all::
-<​code>​ +
-$(EXECUTABLE)$(COMMON_OBJECTS) $(TARGET_OBJECTS) +
-    $(CXX) $(CFLAGS) $(LDFLAGS) $^ -o $@ +
-$(TEST_EXECUTABLE)$(COMMON_OBJECTS) $(TEST_OBJECTS) +
-    $(CXX) $(CFLAGS) $(LDFLAGS) $^ -o $@+
 </​code>​ </​code>​
-  * **$^** object ​file(s) + 
-  * **$@** The file name of the target of the rule.  +Another scenario is to find source .c file in different dirs 
-  * **$%** The target member name, when the target is an archive member+ 
-  * **$<** The name of the first prerequisite. +<code make> 
-<code makefile>​ +build/%.o:: test/%.c 
-.c.o: +        $(COMPILE$(CFLAGS) $< -o $@ 
-    $(CXX) $(CFLAGS) $< -o $@+         
 +build/%.o:: src/%.c 
 +        ​$(COMPILE) $(CFLAGS) ​$< -o $@ 
 +         
 +build/%.o:: lib/src/%.c lib/src/%.h 
 +        $(COMPILE) $(CFLAGS) $< -o $@
 </​code>​ </​code>​
-  * **$?** The names of all the prerequisites that are newer than the target, with spaces between them. For prerequisites which are archive members, only the named member is used (see Archives). ​ 
-  * **$^** The names of all the prerequisites,​ with spaces between them. For prerequisites which are archive members, only the named member is used (see Archives). A target has only one prerequisite on each other file it depends on, no matter how many times each file is listed as a prerequisite. So if you list a prerequisite more than once for a target, the value of $^ contains just one copy of the name. This list does not contain any of the order-only prerequisites;​ for those see the ‘$|’ variable, below. ​ 
-  * $+ This is like ‘$^’, but prerequisites listed more than once are duplicated in the order they were listed in the makefile. This is primarily useful for use in linking commands where it is meaningful to repeat library file names in a particular order. ​ 
-  * $| The names of all the order-only prerequisites,​ with spaces between them.  
-  * $* The stem with which an implicit rule matches (see How Patterns Match). If the target is dir/a.foo.b and the target pattern is a.%.b then the stem is dir/foo. The stem is useful for constructing names of related files. In a static pattern rule, the stem is part of the file name that matched the ‘%’ in the target pattern. 
  
-    In an explicit rule, there is no stem; so ‘$*’ cannot be determined ​in that way. Instead, if the target name ends with a recognized suffix (see Old-Fashioned Suffix Rules), ‘$*’ is set to the target name minus the suffix. For example, ​if the target ​name is ‘foo.c’,​ then ‘$*’ is set to ‘foo’since ‘.c’ ​is a suffix. GNU make does this bizarre thing only for compatibility with other implementations ​of make. You should generally avoid using ‘$*’ except in implicit rules or static pattern rules.+  ​The same target can appear ​in more than one rule 
 +  ​each rule is executed ​if target is older than any prerequisite of that rule 
 +    ​if no prereqcommand ​is always executed 
 +  * Order of execution is not guaranteed
  
-    If the target name in an explicit rule does not end with a recognized suffix, ‘$*’ is set to the empty string for that rule.+====== Parallel make ======
  
-‘$?’ is useful even in explicit rules when you wish to operate on only the prerequisites that have changed. ​For example, suppose that an archive named lib is supposed to contain copies of several object filesThis rule copies just the changed object files into the archive+===== Disabling parallel ===== 
-<​code ​makefile+ 
-lib: foo.bar.o lose.o win.o +For whole file: <code makefile>​.NOTPARALLEL:</​code>​ 
-        ar r lib $?+For single target: 
 + 
 +<​code ​make
 +.NOTPARALLEL:​ %.a  
 +.NOTPARALLEL: foo.bar.
 </​code>​ </​code>​
-Of the variables listed above, four have values that are single file names, and three have values that are lists of file names. These seven have variants that get just the file’s directory name or just the file name within the directory. The variant variables’ names are formed by appending ‘D’ or ‘F’, respectively. These variants are semi-obsolete in GNU make since the functions dir and notdir can be used to get a similar effect (see Functions for File Names). Note, however, that the ‘D’ variants all omit the trailing slash which always appears in the output of the dir function. Here is a table of the variants: 
  
-‘$(@D)’ +[[https://​stackoverflow.com/questions/​8496135/​parallel-makefile-requires-dependency-ordering|Parallel makefile requires dependency ordering]] 
-    The directory part of the file name of the target, with the trailing slash removedIf the value of ‘$@’ is dir/foo.o then ‘$(@D)’ is dir. This value is . if ‘$@’ does not contain a slash. + 
-‘$(@F)’ +===== Prevent one target ​from parallel run ===== 
-    The file-within-directory part of the file name of the target. If the value of ‘$@’ is dir/foo.o then ‘$(@F)’ is foo.o. ‘$(@F)’ is equivalent to ‘$(notdir $@)’.  + 
-‘$(*D)’ + 
-‘$(*F)’ +
-    The directory part and the file-within-directory part of the stem; dir and foo in this example. +
-‘$(%D)’ +
-‘$(%F)’ +
-    The directory part and the file-within-directory part of the target ​archive member name. This makes sense only for archive member targets of the form archive(member) and is useful only when member may contain a directory name. (See Archive Members as Targets.) +
-‘$(<​D)’ +
-‘$(<​F)’ +
-    The directory part and the file-within-directory part of the first prerequisite. +
-‘$(^D)’ +
-‘$(^F)’ +
-    Lists of the directory parts and the file-within-directory parts of all prerequisites. +
-‘$(+D)’ +
-‘$(+F)’ +
-    Lists of the directory parts and the file-within-directory parts of all prerequisites,​ including multiple instances of duplicated prerequisites. +
-‘$(?​D)’ +
-‘$(?​F)’ +
-    Lists of the directory parts and the file-within-directory parts of all prerequisites that are newer than the target.+
  
-Note that we use a special stylistic convention when we talk about these automatic variables; we write “the value of ‘$<​’”,​ rather than “the variable <” as we would write for ordinary variables such as objects and CFLAGS. We think this convention looks more natural in this special case. Please do not assume it has a deep significance;​ ‘$<’ refers to the variable named < just as ‘$(CFLAGS)’ refers to the variable named CFLAGS. You could just as well use ‘$(<​)’ in place of ‘$<​’.