• ArthurSlog

  • SLog-66

  • 1 Year,

  • Through China,

  • Sep 10th 2018

Scan the QR code on wechat and follow my official account

  • ArthurSlog Page

  • GitHub

  • NPM Package Page

  • The nuggets home page

  • Jane books home page

  • segmentfault

The way of heaven is not enough to damage, but the way of supplementing people is not enough to serve


Development Environment MacOS(High Sierra 10.13.5)

Required information and information sources:

  • Make tools

  • Integrated development environment

  • Make tool instruction file Makefile

Start coding

  • Switch to the desktop and create a folder makefile_demo

cd ~/Desktop/ && mkdir makefile_demo

  • Switch to the makefile_demo directory

cd makefile_demo

  • Write a Makefile

Makefile

say_hello:
        echo "Hello Malefile"
Copy the code
  • Then, open the command line and enter a command in the current path

make

  • The result is:
echo "Hello Malefile"
Hello Malefile
Copy the code
  • In the code above;
  1. Say_hello is a method that is executed on the command line,

  2. Echo “Hello Malefile” is the body of the method say_hello. Echo is the system instruction that prints the line “Hello Malefile” on the console

  3. Echo “Hello Malefile” line is not what we want, so we use @ to cancel the output of the command itself

Makefile

say_hello:
        @echo "Hello Malefile"
Copy the code
  • Try again, open the command line and type the command in the current path

make

  • The result is:
Hello Malefile
Copy the code
  • Now write a common Makefile that has three methods

Makefile

say_hello:
	@echo "Hello Malefile"

generate:
	@echo "Create some text file..."touch file-{1.. 10}.txtclean:
	@echo "Cleaning up..."
	rm *.txt
Copy the code
  • Try again, open the command line and type the command in the current path

make

  • The result is still:
Hello Malefile
Copy the code
  • The following two functions are not executed

  • We need to do something extra, change the default method

  • We need to set up an object and fill it with the function to execute, for example: “.DEFAULT_GOAL := generate”

  • The complete code is as follows:

Makefile

.DEFAULT_GOAL := generate

say_hello:
	@echo "Hello Malefile"

generate:
	@echo "Create some text files..."touch file-{1.. 10}.txtclean:
	@echo "Cleaning up..."
	rm *.txt
Copy the code
  • Then, open the command line and enter a command in the current path

make

  • The result is:
Create some text files... touch file-{1.. 10}.txtCopy the code
  • The default method is now generate

  • But the.default_goal object can only run one method at a time

  • If you want to call more than one method, you need to set the all object as follows:

Makefile

all: say_hello generate say_hello: @echo "Hello Malefile" generate: @echo "Creating some files..." touch file-{1.. 10}.txt clean: @echo "Cleaning up..." rm *.txtCopy the code
  • I’m going to delete all the.txt files that I just generated

rm -rf *.txt

  • Then, open the command line and enter a command in the current path

make

  • The result is:
Hello Malefile Create some files... touch file-{1.. 10}.txtCopy the code
  • Ok, now both methods are executed

  • Now, there is a “force all files to update” method

Markfile

.PHONY: all say_hello generate clean

all: say_hello generate

say_hello:
        @echo "Hello Malefile"

generate:
        @echo "Creating some files..."touch file-{1.. 10}.txtclean:
        @echo "Cleaning up..."
        rm *.txt
Copy the code
  • See here, “.phony “has four ways to force all files to update:
  1. Say_hello + generate

  2. say_hello

  3. generate

  4. clean

  • When your make doesn’t work, try setting the value of a “.PHONY “object

  • At this point, a basic Makefile is understood and written.


Please follow my wechat account ArthurSlog

Scan the QR code on wechat and follow my official account

If you like my article, please like it and leave a comment

thank you