How to change the default target of `make`?

The default target of make is the first target. But can I change the default target in Makefile and how to change the default target of make?

The default goal of make is the first target whose name does not start with ‘.’ if .DEFAULT_GOAL is not set. Ref: make manual.

To set the default target to ‘default’ as an example being not the first target in the Makefile, you have at least 2 ways.

Way 1. Set the .DEFAULT_GOAL special variable to ‘default’.

.DEFAULT_GOAL := default

# other targets ...

    @ echo default target

Way 2. clear the .DEFAULT_GOAL special variable before the ‘default’ target so that ‘default’ turns to the first target after .DEFAULT_GOAL is not set.

# other targets ...

# no target starting with '.' should be here
# between .DEFAULT_GOAL and 'default'
    @ echo default target

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *