The introduction

  • A complex project has engineering, compilation usually has a lot of dependencies, at this time, it is necessary to introduce batch construction technology, the famous Docker has Dockerfile, Unix usually use make tools
  • What is make?
  • Make is a build automation tool that looks for makefiles or makefiles in the current directory. If so, the build is done according to the Makefile’s build rules

1. Install make

window

Download link sourceforge.net/projects/mi…

  • 1. Unzip the file and you’ll get a mingW64 folder. Copy it to the directory you want to install.
  • 2. Configure environment variables after decompressing
  • 3, add your mingw64/bin under path, e.g. D:\mingw64\bin
  • 4. Open CMD and run GCC -v
  • Change mingw32-make.exe to make.exe
  • 6. Run make -v

linux

yum install -y make

make -v
Copy the code

Makefile rule

<target> : <prerequisites> 
[tab]  <commands>
Copy the code
  • Target: a user-defined command that you want to execute
  • Prerequisites: Prerequisites that are executed before target, separated by Spaces
  • TAB: Each command must be preceded by a TAB character to indicate that it is a command
  • Commands: Commands performed specifically.
  • PHONY indicates a PHONY command that names files that conflict with the same name and improves performance. If this parameter is added, target is executed when conflicting files exist. Otherwise, an error is reported.
  • With no execution parameters, the first target is executed by default
  • Add @ before command to disable printing of command execution statements and display only execution results
  • ${val} represents variables and is used in the same way as the shell
  • Wildcard characters are allowed
  • ‘#’ indicates a comment

3. Makfile example

The project directory creates a makefile or makefile

# Go parameters BINARY_NAME= go-API BINARY_UNIX=$(BINARY_NAME) _UNIX VERSION="v0.0.1" DATE= 'DATE +%Y%m%d%H% m% S'.PHONY: all all: version test build version: @echo version: ${VERSION} build: @echo version: ${VERSION} date: ${DATE} os: windows @go build -o $(BINARY_NAME) -v -race test: go test tests/* -count=1 clean: go clean rm -f $(BINARY_NAME) rm -f $(BINARY_UNIX) run: go build -o $(BINARY_NAME) -v ./... ./$(BINARY_NAME) # Cross compilation build-linux: @echo version: ${VERSION} date: ${DATE} os: linux-centOS CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BINARY_UNIX) -vCopy the code

Execute the command

make version
make build 
Copy the code

Error: *** missing separator (did you mean TAB instead of 8 Spaces?) . Stop

Solution: Use TAB instead of space

4, summarize

To learn is to earn, and your mutual encouragement

series

  • Serialized a Golang environment build
  • Serial II installation Gin
  • Serial three defines the directory structure
  • Serial four builds case API1
  • Serial five builds case API2
  • Serial six access Swagger interface document
  • Serial seven Log components
  • Serial eight gracefully restarts and stops
  • Serial Makefile build
  • Serial Cron timing mission
  • Serial build command line tools
  • Create a dedicated Cache(First Day)
  • Create your own Cache in 3 days (Second Day)
  • Create a dedicated Cache(Third Day)