In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to talk to you about how to master the knowledge of Makefile, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
If you use macOS or Linux now, you can enter the command man make on the terminal and view the help documentation for the make command, as shown in the following figure:
With the make command, you can quickly run a large number of Shell commands to compile code with one button, format code with one button, and so on.
To learn Makefile, you need to have a Linux or macOS, and then you need to know two concepts: the make command and the Makefile file. Among them, the Makefile file is a text file written by yourself, its name is Makefile, can not be changed case, can only be called this name. Make is a command that comes with macOS and Linux. When we execute the make command, it automatically reads the Makefile file to decide what it wants to do.
Let's look at a practical example. The following is a very simple piece of Golang code:
In the code, there are some commas that are not followed by spaces, and the structures are uneven. When we want to format a .go file, we usually execute the command under the current folder:
Gofmt-w xxx.go
After running, as shown in the following figure:
In order to execute this command, you need to type the keyboard 15 times. And if you have many .go files in your project, and they are in different folders, then you also need to execute the command:
Find. -name "* .go" | xargs gofmt-w
There are even more keypads to be tapped.
At this point, we can create a Makefile file in the project root directory with the following contents:
Fmt: find. -name "* .go" | xargs gofmt-w
As shown in the following figure:
Therefore, when we execute the command: make fmt in the project root directory, all .go files in the entire project are automatically formatted.
The format of the Makefile file is as follows:
Name 1: shell Command 1 shell Command 2 shell Command 3 name 2: shell Command 4 shell Command 5 shell Command 6
Among them, the name 1, name 2 is used to execute the command make name, each name can be followed by a number of Shell commands. This looks a bit like the indentation of Python. However, it is important to note that the indentation of Makefile can only use the Tab key, not spaces.
For another example, now I need to compile the project into an executable file, and then copy the executable file into a folder called output along with data.json. So, our Makefile can write like this:
Fmt: gofmt-w * .go build: rm-rf output mkdir output go build-o JsonReader main.go mv JsonReader. / output/ cp data.json. / output/
Then, when we execute the command make build, the five lines below it are automatically executed at one time.
As another example, there may be some programs that need to be run in the local Docker environment after they have been developed. But if there is already a container with the same name running, we must stop the container and delete the container before we can run it again. But if you have Makefile, it's an one-line command thing:
Deploy: docker build-t xxx:latest docker stop json_reader docker rm json_reader docker run-name json_reader-network host-d xxx:latest
In addition, Makefile also supports concatenation of shell commands under multiple names. For example, if I want to format the code, compile it into an executable file, and then deploy using Docker, our final Makefile file looks like this:
At this point, I just need to execute the command make in the root directory of the project without any parameters, and all the Shell commands under fmt, build, and deploy will be executed sequentially. This greatly reduces our workload.
It can be said that using Makefile to automate and execute some tedious and repetitive commands is an once and for all thing, whether it's Golang projects, Python or other projects.
After reading the above, do you have any further understanding of how to master the knowledge of Makefile? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.