当前位置:网站首页>Tool skill learning (I): pre skills -makfile, make,.Mk
Tool skill learning (I): pre skills -makfile, make,.Mk
2022-07-26 15:28:00 【Little fat man touching his stomach】
Tool skill learning ( One ): Pre skills -makfile、make、.mk
You must have seen a lot when building images makefile file , Yesterday, we also read some build compilation makefile file , But some brothers have no experience in this field , about makefile The format of the file is still not very familiar .
secondly make Is the key command when we compile , By the way, it is related to makefile Origin .
Finally, if your kernel is built to do Android things , There will be many .mk file , So what is this file , Let's have a look .
1、makfile、make、.mk What is it?
makefile It concerns the Compilation Rules of the whole project . There are countless source files in a project , And by type 、 function 、 Modules are placed in several directories ,**makefile A series of rules are defined to specify , Which files need to be compiled first , Which files need to be post compiled , Which files need to be recompiled ,** Even more complex functional operations .
So why is the purpose of this thing obvious –> Because of a large project, tens of thousands of documents , It's hard for you to compile manual links correctly and quickly , Therefore use makefile Unified management .( such as linux Kernel source code)
makefile The benefit is ——“ Automated compilation ”, Once it's written , Just one make command , The whole project compiles completely automatically , Greatly improve the efficiency of software development .make It's a command tool , It's an explanation makefile The command tool of the command in , Generally speaking , Most of the IDE There's an order to
make When the order is executed , Need one makefile file , To tell make How commands need to compile and link programs .
makefile explain make make explain makefile
that .mk What is it? ?.mk It's a kind of android A special “makefile” file ,mk The document will eventually be android Compile the system layer by layer , Turn into make The format that the command can read , To call gcc The compiler compiles , For the time being, before I have a deeper understanding of it , I also regard it as a special field makefile file . It's just through android A lot of processing of compiling system ,android.mk The format of becomes very simple , And with ordinary makefile The writing format of the document is different .
( A mature development framework must have its own packaging system , such as C or C++ In the project make file 、Java In the project ant Tools etc. ,NDK It also has its own packaging configuration file , These documents mk For the suffix .Android.mk File is NDK Compiled script for , Used for holding C or C++ Code compiled into so file .)
(Android NDK The full name is Android Native Development Kit, seeing the name of a thing one thinks of its function , Is the native code call , In fact, it allows Java Procedure passed JNI call C or C++ Dynamic link library for (so file ).)
The following is divided into three parts to talk about the specific use , If your embedded system is not Android , Maybe you don't need to see this .mk, But as a technologist , There is nothing wrong with learning more .
2、makefile file
Want to write a complete Makefile file , Need to know Makefile The relevant writing rules of . We already know that 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
command
Or is it
targets : prerequisites; command
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 mark
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 .
From the above example, we can see that ,Makefile The rules are simple , But it's not Makefile All , This is just the tip of the iceberg . Just one rule can't satisfy our compilation of large engineering projects . Even the compilation of several files will have problems , So there's a lot to learn .
In a nutshell Makefile The content in , It mainly consists of five parts , Namely :
1) Explicit rules
The explicit rule states , How to generate one or more target files . This is from Makefile The writer of the book clearly points out that , Files to generate , Files dependent on files , Generated commands .
2) The rules are obscure
Because of our make Naming has the function of automatic derivation , So the obscure rules allow us to write roughly and briefly Makefile, This is from make What the command supports .
3) Definition of variables
stay Makefile We're going to define a series of variables in , Variables are usually strings , This is a bit like C Macro in language , When Makefile When executed , All the variables will be extended to the corresponding reference positions .
4) The document indicates
It consists of three parts , One is in a Makefile Reference another Makefile, It's like C In language include equally ; The other is to specify according to certain circumstances Makefile The effective part of , It's like C Precompiling in languages #if equally ; There is also the definition of a multi line command .
5) notes
**Makefile There are only line comments in , and UNIX Of Shell The script is the same , Its annotation is with “#” character ,** This is like C/C++ Medium “//” equally . If you want to be in your Makefile Use in “#” character , You can escape with a skew box , Such as :“#”.
The content is really rich , Many predecessors have given good tutorials , No more repetition
Recommended learning materials : Teacher Ruan Yifeng taught you how to write makefile
[ Write with me Makefile](https://blog.csdn.net/haoel/article/details/2886)
3、make command
make It's just a designated Shell Command to build tools . Its rules are simple , You specify which file to build 、 Which source files does it depend on , When those documents change , How to rebuild it .
1、 Easy to use make Or in make Take the target after the command all
2、 adopt -B Option to always re-establish all goals
3、 Use -d Option to print debugging information
4、 Use -C Option to change the directory
5、 adopt -f Option treats other files as Makefile
For the study of tools, I hope to see simple things , I have a general understanding , You can gradually become familiar with complexity in actual use , Of course, everyone is suitable for different ,just follow your heart.
4、.mk File format
(1)LOCAL_PATH:=$(call my-dir)
LOCAL_PATH Is each Android.mk Files must be defined , Used to specify the root directory of the project , The compiler will look for code source files in this directory tree . in addition ,“call my-dir” Statement returns the current directory path .
(2)include$(CLEAR_VARS)
include Syntax is used to include external libraries (C Library),CLEAR_VARS Provided by the compiling system , Corresponding GNU Makefile The script will clear for us LOCAL_PATH All other than LOCAL_ Variable with prefix , Such as LOCAL_MODULE、LOCAL_SRC_FILES、LOCAL_STATIC_LIBRARIES etc. .
This is necessary , Because all the compile control files are in the same GNU Make Environment .
(3)LOCAL_MODULE:=hello-jni
LOCAL_MODULE It must also be defined , Used to identify C Each module in the project , The name of the final link library file is also related to this value , For example, it is generated here so The file name is libhello-jni.so. in addition ,LOCAL_MODULE Value must be the only , And cannot contain spaces .
(4)LOCAL_SRC_FILES:=hello-jni.c
LOCAL_SRC_FILES Is used to specify the C Project source code file , Of course, if you include multiple files, you can use “\” Wrap symbols . There is no need to include header files , The system will automatically prepare for us . in addition , If you want to use different C++ file name , Can be configured by LOCAL_DEFAULT_CPP_EXTENSION Parameter to specify .
(5)include$(BUILD_SHARED_LIBRARY)
BUILD_SHARED_LIBRARY Also provided by the compilation system , Corresponding GNU Makefile The script will collect all for us with LOCAL_ Variable with prefix , This allows C The class library and code of the project are clearer , And we can also configure parameters BUILD_STATIC_LIBRARY To generate static libraries . such as , Use “include$(BUILD_STATIC_LIBRARY)” Will be generated with .a Static library for suffix .
Of course , In addition to the common system variables described above , Such as CLEAR_VARS、BUILD_SHARED_LIBRARY as well as BUILD_STATIC_LIBRARY outside ,Android.mk The file also supports the following variables .
TARGET_ARCH: Is used to specify the CPU type , Common values are arm and x86.
TARGET_PLATFORM: Is used to specify the Android Version of the platform .
TARGET_ARCH_ABI: Is used to specify the CPU+ABI The type of , such as armeabi On behalf of Armv5TE The instruction set architecture of . Although there are only two types supported at present , But in the future NDK There may be more choices in the version .
then , Let's take another look at the common module description variables , Such as LOCAL_PATH、LOCAL_MODULE、LOCAL_SRC_FILES as well as LOCAL_CPP_EXTENSION outside ,Android.mk The file also supports variables .
LOCAL_C_INCLUDES: optional , Express C or C++ Search path of header file , Generally, it is the relative path of the project directory .·
LOCAL_CFLAGS: Optional compilation options , Compiling C Code using , It is useful when using add-on packages or macro definitions , such as LOCAL_CFLAGS:=-DHHH Equivalent to #define HHH.·
LOCAL_CXXFLAGS: Same as LOCAL_CFLAGS, It's just for C++ Code .·
LOCAL_CPPFLAGS: Same as LOCAL_CFLAGS, Yes C perhaps C++ The code applies .·
LOCAL_STATIC_LIBRARIES: Represents the static library needed for module compilation .·
LOCAL_SHARED_LIBRARIES: Represents the shared library needed for module compilation ( Dynamic library ).·
LOCAL_LDLIBS: Linker options to use when compiling , such as -lz It means you need to link to libz.so library .
Last , Learn more Android.mk Macro definitions available in the file .·
my-dir: Return to the current directory .·
all-subdir-makefiles: Return all subdirectories Android.mk Path list of files .·
this-makefile: Returns the current Android.mk Path to file .·
parent-makefile: Returns the parent of the number of calls Android.mk File path .
APP_PROJECT_PATH: It is used to give an absolute path to the root directory of the application project .·
APP_MODULES: Used to list all modules required by the application , If there is no definition ,NDK Will be right Android.mk Compile the default module declared in .
APP_OPTIM: The optional modes are release or debug Two kinds of , It indicates whether the compiled application is a release version or a debug version . If it's a release (release Pattern ), The compiler will generate more optimized binaries , Conducive to operation ; While the debug version (debug Pattern ) Is more conducive to debugging .·
APP_CFLAGS: Same function Android.mk Of LOCAL_CFLAGS.·
APP_CXXFLAGS: Same function Android.mk Of LOCAL_CXXFLAGS.·
APP_CPPFLAGS: Same function Android.mk Of LOCAL_CPPFLAGS.·
APP_BUILD_SCRIPT: Specify the compilation script , By default NDK The compiler will be in the project jni Search the directory named Android.mk The file of .·
APP_ABI: Select instruction set , Options include armeabi、armeabi-v7a as well as x86.
The above is the understanding of this part , I suggest that you learn tools based on requirements , Otherwise, it will come quickly , It's super fast to forget .
边栏推荐
- Chuhuan technology is listed on Shenzhen Stock Exchange: Minsheng securities, with a market value of 2.7 billion, is a shareholder
- pytorch---进阶篇(函数使用技巧/注意事项)
- 筑牢生态安全防线,广州开展突发环境事件应急演练
- R语言使用lattice包中的histogram函数可视化直方图(histogram plot)、col参数自定义填充色、type参数自定义直方图显示模式(计数或者比例)
- Which software must be used by scientific researchers to read literature?
- Enterprise digital transformation needs in-depth research, and it cannot be transformed for the sake of transformation
- FOC learning notes - coordinate transformation and simulation verification
- C# 给Word每一页设置不同文字水印
- The most detailed patent application tutorial, teaching you how to apply for a patent
- Practical task scheduling platform (scheduled task)
猜你喜欢

Where is the foreign literature needed to write the graduation thesis?

【5分钟Paper】Pointer Network指针网络

What is the transport layer protocol tcp/udp???

Practical purchasing skills, purchasing methods of five bottleneck materials

The most detailed patent application tutorial, teaching you how to apply for a patent

NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】

Credit card number recognition (openCV, code analysis)
![[leetcode daily question] - 268. Missing numbers](/img/96/dcf18522257dea7cb7e52f5bb7ced3.png)
[leetcode daily question] - 268. Missing numbers

University rankings in Beijing

Yifang biological fell 16% on the first day of listing: the company's market value was 8.8 billion, and Hillhouse and Lilly were shareholders
随机推荐
DICOM learning materials collection
【LeetCode每日一题】——268.丢失的数字
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
R语言wilcox.test函数比较两个非参数样本的总体的中心位置是否具有显著差异(如果两个样本数据是配对数据设置paired参数为TRUE)
VS添加作者信息和时间信息的设置
Jintuo shares listed on the Shanghai Stock Exchange: the market value of 2.6 billion Zhang Dong family business has a strong color
R language wilcox The test function compares whether there is a significant difference in the central position of the population of two nonparametric samples (if the two sample data are paired data, s
Enterprise digital transformation needs in-depth research, and it cannot be transformed for the sake of transformation
Deep Packet Inspection Using Quotient Filter论文总结
Prometheus adds redis and MySQL node monitoring
DICOM学习资料收集
关于工控网关物联网串口转WiFi模块与串口转网口模块的选型
About the selection of industrial control gateway IOT serial port to WiFi module and serial port to network port module
Parallel d-pipeline: a cuckoo hashing implementation for increased throughput
anaconda No module named ‘cv2‘
FOC motor control foundation
Operation method of abbkine elikine human alpha fetoprotein (AFP) ELISA quantitative Kit
USB转串口参数配置功能
北京的大学排名
sqlDeveloper工具快速入门