Table of Contents
1. What is Makefile
2. Advantages of Makefile
3. Naming rules
4. Basic rules
7. Ordinary variables
8. Automatic variables
9. Function

What is makefile in linux

Nov 26, 2021 pm 02:02 PM
linux makefile

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.

What is makefile in linux

#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 generated

Dependencies--> Some files required to generate the target

Rules--> Means of generating target files from dependency files

tab -->

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:

test: test.c
    gcc test.c -o test
Copy after login

Among them, test in the first line is the target to be generated, test.c is the dependency, and the second line is the rule for generating test from test.c.

Sometimes there are multiple goals in the Makefile, but the Makefile will

set the first goal as the ultimate goal.

5. Working principle

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

What is makefile in linux

For example, in the picture above, the rule for generating calculator is gcc main.o add.o sub.o mul.o p.o -o, Makefil will check main.o first, Whether add.o, sub.o, mul.o, p.o exists, if not, it will look for rules to generate the dependency file.

For example, if the dependency of main.o is missing, the Makefile will look below to see if there are rules to generate main.o. When it finds that the rule gcc main.c -o main.o can generate main.o, it uses this rule to generate main.o, and then generates the ultimate goal calculator.

The whole process is to search for dependencies downwards, and then execute commands upwards to generate the ultimate goal.

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.

What is makefile in linux

For example, if main.c is modified, the main.o target will be recompiled. When main.o is updated, the ultimate target calculator will also be recompiled. The update of other files is also done by analogy.

6. Command execution

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_demo

make 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

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)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles