What is makefile in linux
In Linux, makefile is the compilation rule of a project file, which describes the compilation and linking rules of the entire project; it includes which files need to be compiled, which files do not need to be compiled, and which files need to be compiled first. Which files need to be post-compiled, which files need to be rebuilt, etc.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
1. What is Makefile
Makefile can be simply thought of as the compilation rules of a project file, describing the compilation and linking rules of the entire project.
An enterprise-level project usually has many source files. Sometimes they are classified into different directories according to functions, types, and modules. Sometimes multiple programs are stored in one directory. source code.
Makefle was born to solve the problem of how to compile some of the above codes. It defines a set of rules that determine which files should be compiled first, which files should be compiled later, and which files should be recompiled.
Everything involved in compiling the entire project can be described in the 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.
The advantage of Makefile is that it can achieve "autonomous compilation". The entire project usually only needs one make command to complete compilation, linking, and even more complex functions. It can be said that any Linux source program comes with a Makefile.
2. Advantages of Makefile
Manage the compilation of the code, decide what files to compile, the compilation order, and whether recompilation is needed;
Save compilation time. If the file changes, you only need to recompile this file without recompiling the entire project;
once and for all. Makefiles usually only need to be written once and do not need to be changed too much later.
3. Naming rules
Generally speaking, it is OK to name the Makefile as Makefile or makefile, but many sources The name of the file is lowercase, so more programmers use the name of the Makefile because it can display the Makefile first.
If you name the Makefile with another name, such as Makefile_demo, it is also allowed, but you should use it in the following way:
make -f Makefile_demo
4. Basic rules
Makefile's basic rules are:
Series Objective: dependence
(TAB) rule
## Objective --> Target files that need to be generatedDependencies--> Some files required to generate the targetRules--> Means of generating target files from dependency filestab -->
Each rule must start with tab, spaces are not allowed
For example, we often write gcc test.c -o test, Using Makefile, it can be written as:1 2 |
|
set the first goal as the ultimate goal.
5. Working principleGeneration of target:
a. Check whether the dependent files in the rules exist;b . If the dependent file does not exist, look for rules to generate the dependent file.Update of the target:
a. Check all the dependencies of the target. If any dependency is updated, regenerate the target; b. If the target file is later than the dependent file, it needs to be updated.make:
Use this command to generate the target file according to predetermined rules. If the name of the Makefile file is not Makefile or makefile, the-f option should be added, such as:
make -f Makefile_demomake clean:
Clear the intermediate files (.o files) and final target files generated during the compilation process.
If a file named clean exists in the current directory, this command will not be executed.
-->Solution: Pseudo target declaration: .PHONY:clean
Special symbols:
-: Indicates that even if an error occurs during the execution of this command, subsequent commands will continue to be executed. For example:
-rm a.o build/
@: indicates that the command will only be executed without echoing. When a general rule is executed, the executing rule will be printed out on the terminal. After adding this symbol, only the command will be executed and the executed rule will not be echoed. For example:
@echo $(SOURCE)
7. Ordinary variables
Variable definition and assignment:
Variables are directly used The definition can be completed by assigning a value, such as:
INCLUDE = ./include/
Variable value:
Surround it with brackets and add dollar sign, such as:
FOO = $(OBJ)
The system comes with variables:
is usually uppercase, such as CC, PWD, CFLAG,etc.
Some have default values, some do not. For example, the common ones:
CPPFLAGS: Options required by the preprocessor such as: -I
CFLAGS: Parameters used during compilation –Wall –g -c
LDFLAGS : The option used by the link library -L -l
The default value of the variable can be modified. For example, the default value of CC is cc, but it can be modified to gcc: CC=gcc
8. Automatic variables
Commonly used automatic variables:
Makefile provides many automatic variables, but the following three are commonly used. These automatic variables can only be used in commands in rules and cannot be used elsewhere.
$@ --> Target in the rule
$ First dependency condition in the rule
$^ --> Rule All dependency conditions in
For example:
app: main.c func1.c fun2.c
gcc $^ - o $@
where: $^ represents main.c func1.c fun2.c, $
Pattern rules:
Pattern rules use % in the target and dependency conditions to match the corresponding files, such as main.c, func1.c in the directory , func2.c three files, the compilation of these three files can be completed by one rule:
%.o:%.c
$(CC) –c $
This pattern rule means:
main.o is generated by main.c,
func1.o is generated by func1.c,
func2.o is generated by func2.c
This is the role of pattern rules, which can match all files in the directory at one time.
9. Function
makefile also provides us with a large number of functions, and the following two functions are also frequently used. It should be noted that all functions in the makefile must have a return value. In the following example, suppose there are three files main.c, func1.c, and func2.c in the directory.
wildcard:
is used to find files of a specified type in a specified directory. The following parameter is the directory file type, for example:
src = $ (wildcard ./src/*.c)
This sentence means: Find all files with the suffix .c in the ./src directory and assign them to the variable src.
After the command execution is completed, the value of src is: main.c func1.c fun2.c.
patsubst:
Matching replacement, such as the following example, is used to find all files ending in .c from the src directory and replace them with .o files, And assign it to obj.
obj = $(patsubst %.c ,%.o ,$(src))
Replace all files with a .c suffix in the src variable with .o.
After the command is executed, the value of obj is main.o func1.o func2.o
Specially, if you want to place all .o files in the obj directory, you can use the following method:
ob = $(patsubst ./src/%.c, ./obj/%.o, $(src))
Related recommendations: "Linux Video Tutorial》
The above is the detailed content of What is makefile in linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
