当前位置:网站首页>Makefile notes (Yiwen Institute makefile)
Makefile notes (Yiwen Institute makefile)
2022-06-30 23:32:00 【Embedded Yuexiang Garden】
Makefile Detailed explanation
There are countless source files in a project , By type 、 function 、 Modules are placed in several directories ,Makefile Defines a set of rules for which files to compile first , Which files need to be post compiled , Which files need to be recompiled , Even more complex functional operations , because Makefile Like a Shell The script is the same , You can also execute commands from the operating system .
1、Makefile Regular format
Want to master makefile, First, we need to understand two concepts ,⼀ Yes ⽬ mark (target), another ⼀ One is dependence (dependency).⽬ Mark means ⼲ what , Or luck ⾏ make after ⽣ What is it ,⽽ Dependence is telling make How to do it to achieve ⽬ mark . stay Makefile in ,⽬ Targets and dependencies are through rules (rule) To express the .
The goal is XX1: Dependency file
<TAB> command 1
<TAB> command 2
The goal is XX2: Dependency file
<TAB> command 1
<TAB> command 2
... ...
Instructions 1:
command 1
command 2
Instructions 2:
command 1
command 2
... ...
Example :
Tag:a.o b.o c.o
gcc -o Tag a.o b.o c.o
a.o: a.c
gcc -c a.c
b.o: b.c
gcc -c b.c
c.o: c.c
clean:
rm .o
rm Tag



2、 Rule description
Makefile Describes the rules of file compilation , Its rules are mainly composed of two parts , They are dependency relations and commands executed , Its structure is as follows :
targets: prerequisites
[Tab]command
The instructions are as follows :
targets: The goal of the rules , It can be Object File( It's called an intermediate file ), It can also be an executable file , It can also be a label ;prerequisites: It's our dependency file , To generate targets The required files or targets . It can be more than one , It can also be No ;command:make Commands that need to be executed ( Any of the shell command ). There can be multiple commands , Each command takes one line .
Be careful : We should use colon to separate our target and dependent files , The beginning of the command must use Tab key .
Let's use the following example Makefile The rules of ,Makefile The code added to the file is as follows :
test:test.c
gcc -o test test.c
The function of the above code is to compile test.c file , Through this example, we can explain in detail Makefile The specific use of . among test Yes, the target file , It's also our final executable .
Dependent files are test.c Source file , What you need to do to rebuild the target file is gcc -o test test.c. This is it. Makefile The use of basic grammatical rules of . Use Makefile The way : First, you need to write Makefile file , And then in shell In the implementation of make command , The program will execute automatically , Get the final target file .
3、C File compilation process
-E : Preprocessing --> take .h .c Expand to form a file , And generate .i file ;
gcc -E hello.c -o hello.i
-S : compile --> The pretreated .i Compile the file into an assembly file .s;
gcc -S hello.i -o hello.s
-c : assembly --> Will be compiled .s Files are compiled into machine language .o;
gcc -c hello.s -o hello.o
-o : machine language --> The functions in the object file 、 Variables, etc ;
gcc hello.o -o hello
File format change process :
.c --> .i -->.s --> .o --> The goal is
4、 Use of pseudo targets
.PHONY yes Makefile Keywords of the file , Indicates that the targets in the list behind it are all pseudo targets .
Generally, pseudo targets are used to write cleanup statements , For example, the following statement :
.PHONY:clean
clean:
rm -rf hello main.c func1.o func2.o
because , We don't generate clean This file . Fake target It's not a document , It's just a label , because “ Fake target ” It's not a document , therefore make Unable to generate its dependencies and determine whether it will execute . We can only indicate this by showing The goal is To make it work . Of course , Fake target The name of cannot be the same as the file name , Otherwise, it will lose Fake target That's the meaning of .
5、 many .c Of documents Makefile
For example, there are files :a.cb.cd.ce.c Need to finally get main
Then the code writing format is as follows :
main:a.o b.o d.o e.o
gcc a.o b.o d.o e.o -o main
a.o:a.c
gcc -c a.c -o a.o
b.o:b.c
gcc -c b.c -o b.o
d.o:d.c
gcc -c d.c -o d.o
e.o:e.c
gcc -c e.c -o e.o
6、 Use of variables
These are commonly used predefined variables .
$* Target file name without extension .
$+ All dependent files , Separate... By spaces , And in the order of emergence , May contain duplicate dependency files .
$< The name of the first dependency file .
$? All dependent files , Separate... By spaces , The modification date of these dependent files is later than the creation date of the target .
[email protected] The full name of the target .
$^ All dependent files , Separate... By spaces , Does not contain duplicate dependency files .$% If the target is an archive member , Then the variable represents the archive member name of the target .
One of the most common is [email protected]$^ these two items. , We modify the above code with variables as follows :
TAR =main
OBJ = main.o a.o b.o c.o
CC := gcc
RMRF := rm -rf
$(TAR):$(OBJ)
$(CC) $^ -o [email protected]
%.o:%.c
$(CC) -c $^ -o [email protected]
# This sentence can replace all statements in the same format ;
.PHONY:
clear:
$(RMRF) $(OBJ)
among TAR =mainOBJ = main.o a.o b.o c.oCC := gccRMRF := rm -rf For custom variables , Other zones $ The of the symbol is the system predefined variable .
7、 The rules are obscure
More complicated Makefile We can't write one by one , Then the obscure rules show his power , Let's take a look at the specific use of obscure rules .
According to the above structure description , We are right. Makefile Rewriting layer by layer , The first is the obscure rule , Tell you one of the usages :
TAR =main
OBJ = main.o a.o b.o c.o
CC := gcc
RMRF := rm -rf
$(TAR):$(OBJ)
$(CC) $^ -o [email protected]
%.o:%.c
$(CC) -c $^ -o [email protected]
# This sentence can replace all statements in the same format ;
.PHONY:
clear:
$(RMRF) $(OBJ)
The following sentences are the writing of obscure rules , We can use these words instead of hundreds of lines of code .
%.o:%.c
$(CC) -c $^ -o [email protected]
This obscure rule actually tells us , The suffix is .c How to compile the file into .o, The suffix is c How to compile the file into .o.
So far Makefile There are already models and samples !, Now let's share the specific code execution process :
8、 At the end
Last , Add a few points to write Makefile Precautions for
1. tab Separate , You can't use spaces .
2. Every makefile It's better to add one all
3. With notes “#” Symbol
4. The document indicates , Quote other makefile file
边栏推荐
- PS2 handle-1 "recommended collection"
- 35 giant technology companies jointly form the meta universe standard Forum Organization
- Repetition is the mother of skill
- Query points in MATLAB Delaunay triangulation
- C# /platform:anycpu32bitpreferred 只能与 /t:exe、/t:winexe 和 /t:appcontainerexe 一起使用
- Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
- Solve arm_ release_ ver of this libmali is ‘g2p0-01eac0‘,rk_ so_ Ver is' 4 ', libgl1 mesa dev will not be installed, and there are unsatisfied dependencies
- Error when starting PHP: [pool www] cannot get uid for user '@php_ fpm_ [email protected]’
- Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908
- How to ensure the security of our core drawings by drawing encryption
猜你喜欢

206 page Shanghai BIM Technology Application and development report 2021

HP notebook disable touchpad after mouse is inserted

Development of wireless U-shaped ultrasonic electric toothbrush

The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?

Swift 5.0 - creation and use of swift framework

Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction

76页智慧物流园区综合解决方案2022(附下载)

Esp8266 becomes client and server

Ride: get picture Base64

Kubevela 1.4: make application delivery safer, easier to use, and more transparent
随机推荐
2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main()
Doker's container data volume
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
CNN classic network model details -lenet-5 (pytorch Implementation)
微信支付WxPayPubHelper v3版 回调xml为空的原因
[golang] golang实现截取字符串函数SubStr
Is it safe to buy funds on the compass?
[golang] golang implements the string interception function substr
机器学习编译入门课程学习笔记第二讲 张量程序抽象
企业出海数字化转型解决方案介绍
股票开户要如何办理呢?办理手机开户安全吗
flutter - sort List排序
Fund customer service
Development of wireless U-shaped ultrasonic electric toothbrush
Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance
需求评审,测试人员应该发挥怎样的价值?两分钟让你不再懵逼
1175. 质数排列 / 剑指 Offer II 104. 排列的数目
How do it outsourcing resident personnel position their pain points?
Don't worry about whether you can be a coder if you don't learn English well. Learn it first
Warmup preheating learning rate "suggestions collection"