Default Target in Makefile: How Make App Knows Which Target to Build?
When executing a makefile, the make app must know which target to build. By default, make will attempt to build the first target listed in the makefile. However, it is possible to specify a default target using the special variable .DEFAULT_GOAL.
To set a default target, simply add the following line to the makefile:
.DEFAULT_GOAL := target_name
Replace "target_name" with the name of the desired default target.
It is important to note that if the user specifies a different target when running make, that target will take precedence over the default target specified in the makefile.
In addition to .DEFAULT_GOAL, make also recognizes the special target .PHONY. This target is used to specify targets that are not actual files, but rather actions to be performed. For example:
.PHONY: clean
clean:
rm *.o
In this example, the target "clean" is specified as .PHONY because it is not an actual file, but rather a command to remove object files.
In summary, the default target in a makefile is specified using the .DEFAULT_GOAL variable. By default, make will attempt to build the first target listed in the makefile. The .PHONY target is used to specify targets that are not actual files, but rather actions to be performed.
Leave a Reply
Related posts