Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Makefile in Linux

Shulou Source: shulou.com Published: 2022-06-02 00:01:31 09月16日 Update

This article mainly explains "how to use Makefile in Linux". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Makefile in Linux".

In a nutshell, the function of Makefile is "automatic compilation". When we have written the Makefile of the entire project, we only need a simple make command to achieve automatic compilation.

Naming of 1.makefile (two kinds)

Makefile

Makefile

2. Rules of makefile

Three elements of a rule: goal, dependency, and command

3. The writing of multi-file makefile (a source file only recompiles the changed file)

Makefile can have multiple rules, and when the command of the first rule is executed without a corresponding dependency, look for it in the following rule. The goal of the top rule is that the ultimate goal must be written at the top, that is, the final file to be generated.

Update principle of multi-file Makefile (based on time)

4. Variables in makefile

Custom variable

Obj=main.o add.o sub.o

Use $(obj) directly when referencing

Automatic variable ()

$@: target in the rule

$^: all dependencies in the rule

Automatic pattern matching%

In the subrules:

Variables maintained by makefile (usually uppercase and can be modified by yourself)

CC:cc (i.e. gcc)

APPFLAGS: options used for preprocessing

CFLAGS: options to use when compiling

LDFLAGS: options used by linked libraries

5. Functions in makefile (all have return values)

Wildcard finds all .c files in the current directory and returns the value to src

Src=$ (wildcard. / * .c)

Patsubst replaces all .c files with .o files

Obj=$ (patsubst. /% .c,. /% .o, $(src))

6. Make clean

Add the goal of clean at the end of makefile. In order to recompile all files, you have to delete the original generated files.

7. The final simple makefile

Thank you for your reading, the above is the content of "how to use Makefile in Linux". After the study of this article, I believe you have a deeper understanding of how to use Makefile in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Files rules goals compilations variables commands times learning content that is faceted generated three that is functions functions principles multiple uppercase ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Huawei vpn MySQL