Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Makefile Rule File by Go

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Go how to write Makefile rules file", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Go how to prepare Makefile rules file" article.

Make introduction

With the help of Makefile, we no longer need to manually enter the compiled commands and parameters in the compilation process, which can greatly simplify the project compilation process.

Make is a build automation tool that looks for Makefile or makefile files in the current directory. If there is a corresponding file, it completes the build task according to the rules defined in it.

Makefile introduction

We can simply think of Makefile as it defines the compilation rules for a project file. With the help of Makefile, we no longer need to manually enter the compiled commands and parameters in the compilation process, which can greatly simplify the project compilation process. At the same time, using Makefile can also determine specific compilation rules and processes in the project, and Makefile files are defined in many open source projects.

This article will not cover the various rules of Makefile in detail, but will only give examples of Makefile that are commonly used in Go projects.

Makefile file tutorial parsing

Our tutorial is mainly about Makefile. Many beginners of Linux (Unix) development do not understand what Makefile is, and even most Windows development engineers are particularly unfamiliar with Makefile. This is actually quite normal, if you are developing under Windows, then you do not need to consider this problem, because the integrated development environment (IDE) under Windows has built-in Makefile, or will automatically generate Makefile, we do not have to write manually.

However, this cannot be done in Linux. We need to finish the work manually. There are many development languages that can be learned under Linux. The common ones are Cpicardic + language, python, java and so on. If you want to do development under Linux (Unix), not knowing Makefile is a very failed thing, even if you can't become a qualified Linux development engineer. If you do not understand Makefile, you will not be able to operate multi-file programming, and you will not be able to complete operations relative to large engineering projects. Makefile is a skill you must master if you want to develop in a Linux (Unix) environment.

So, what on earth is Makefile?

Makefile can be simply thought of as a compilation rule of a project file, which describes the compilation and linking rules of the whole project. It contains files that need to be compiled, files that don't need to be compiled, files that need to be compiled first, files that need to be compiled later, files that need to be rebuilt, and so on.

What needs to be involved in compiling the whole project can be described in Makefile. In other words, Makefile can automate the compilation of our project projects without having to manually enter a bunch of source files and parameters every time.

Take the C language development under Linux as an example to illustrate that multiple files are compiled to generate a file, and the compiled commands are as follows:

Gcc-o outfile name1.c name2.c...

The name of the executable program to be generated by outfile, and nameN.c is the name of the source file. This is an example of compiling C files using the gcc compiler under Linux. If we do not encounter a large number of source files, we can choose this way of compilation. If there are too many source files, you will encounter the following problems.

1) the problem of linking libraries when compiling. Take C, for example, when compiling, gcc will only link some basic C standard libraries by default, and many of the standard libraries on which the source files depend need to be linked manually.

Here are some standard libraries that we need to link manually:

Name1.c uses the functions in the mathematical library math. We have to add the parameter-Im manually.

Name4.c uses functions from the small database SQLite, so we have to add the parameter-lsqlite3 manually

Name5.c uses threads, and we need to add the parameter-lpthread manually.

Because there are a lot of files, but also to link a lot of third-party libraries. So when compiling, the commands will be very long, and when compiling, we may involve the order of file links, so manual compilation will be very troublesome.

It will be different if we learn to use Makefile, which will completely simplify the compilation operation. Put the library files to be linked in Makefile, and make the corresponding rules and the corresponding link order. This only needs to be done by executing the make command, and the project will be compiled automatically. It is very convenient to execute make every time you want to compile the project, omitting the parameter options and commands in manual compilation.

2) it takes a long time to compile a large project.

If we do project development, we will inevitably have to modify the source file of the project and recompile it after each change. A large project can have more than several source files, the number of source files may be hundreds or thousands. For example, a kernel, or a software source package. These are what we often encounter when doing development. It takes us a little bit of time to compile such a file. If the file is very large, it may take us half a day.

Can we solve such a problem with Makefile? Of course, Makefile supports multithreaded concurrent operations, which will greatly shorten our compilation time, and when we modify the source file, compile the entire project, the make command will only compile our modified files, no modified files do not need to recompile, but also greatly solve our time-consuming problem.

In fact, this is a relatively common problem we encounter, of course, there may be many problems, such as: there are many types of source files in project files, so you need to choose a compiler when compiling; files may be distributed in different directories, and price adjustment paths are required when you use them. All these problems can be solved through Makefile. And the Makefile in the file only needs to be completed once, generally, as long as we do not add or delete the file in the project, Makefile basically does not need to modify, compile with only a make command. It provides us with great convenience and greatly improves the efficiency of compilation.

Overview of Rul

Makefile consists of several rules, each of which is mainly composed of two parts, namely, the dependent relationship and the executed command.

Its structure is as follows:

[target]...: [prerequisites]... [command]......

Where:

Targets: the goal of the rule

Prerequisites: optional files or targets needed to generate targets.

The command that command:make needs to execute (any shell command). There can be multiple commands, each on a single line.

For example:

Build: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build-o xx example. PHONY: all build run gotool clean helpBINARY= "bluebell" all: gotool buildbuild: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build-o {BINARY} run: @ go run. / gotool: go fmt. / go vet. / clean: @ if [- f ${BINARY}]; then rm ${BINARY} Fihelp: @ echo "make-formats the Go code and compiles the binary" @ echo "make build-compiles the Go code Generate binaries "@ echo" make run-directly run Go code "@ echo" make clean-remove binaries and vim swap files "@ echo" make gotool-run the Go tool 'fmt' and' vet' "above is the content of this article on" how Go writes Makefile rule files " I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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: 251

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report