Home > System Tutorial > LINUX > body text

Performance optimization of Linux Makefile: How to improve compilation speed and efficiency

WBOY
Release: 2024-02-09 14:06:19
forward
867 people have browsed it

Makefile is a commonly used file format in Linux systems. It can be used to describe the compilation process and dependencies of a program, allowing users to easily use the make command to build and manage programs. The advantages of Makefile are simplicity, flexibility, and portability, but it also has some disadvantages, such as slow compilation speed, high resource consumption, and repeated compilation. So, how to optimize the performance of Makefile and improve compilation speed and efficiency under Linux? This article will introduce you to some performance optimization methods for Linux Makefiles, such as using parallel compilation, incremental compilation, precompiled header files, caching compilation results, etc., so that you can better use and write Makefiles under Linux.

Linux Makefile 的性能优化:如何提高编译速度和效率

1. The impact of assignment operators

How do the two assignment operators ‘:=’ and ‘=’ affect compilation speed? The secret lies in their essential meaning. If you understand it thoroughly, you will not have any doubts. The blogger also thought hard for a long time and searched a lot of Internet information, and finally was awakened by the words of this blog post! Please click here to view the blog post

  • = assignment operator – recursive expansion – delayed expansion
    "=" means to expand the entire Makefile before determining the value of the variable. That is, the value of the variable will be the value specified by the last recursive expansion in the entire Makefile, regardless of whether the intermediate variable it refers to is before or after the target rule is executed. .

Examples are as follows:

foo = $(bar)
bar = $(ugh)
ugh = $(Huh)

all:
    @echo $(foo)

Huh = AfterTarget
Copy after login
make之后其结果为 : AfterTarget
其整个执行过程如下:
    首先“$(foo)”被替换为“$(bar)”,
    接下来 “$(bar)”被替换为“$(ugh)”,
    “$(ugh)”被替换为“$(Huh)”,
    最后$(Huh)被替换为“AfterTarget”。
    整个替换的过程是在执行“echo $(foo)”是进行的。
Copy after login

Note: The entire replacement process is performed when "echo $(foo)" is executed.

  • := assignment operator – direct expansion – immediate expansion
    ":=" means that the value of the variable is determined by its position in the Makefile, rather than the final value after the entire Makefile is expanded. When using this assignment operator to define a variable, the reference to another variable or function in the variable value is expanded when it is defined, that is, when it is defined, the variable is already the final value of its expression.

Examples are as follows:

x := foo
y := $(x) bar
x := xyz
Copy after login

In the above example, the value of y will be foo bar instead of xyz bar.


The above is the basic description of these two assignment operators. The following is a detailed description of how they affect the compilation speed!

Examples are as follows:

TmpDir = /Source  #此处随意定义了一个目录,
                  #但切记在实际编写的 Makefile 中,不要在赋值语句后面写注释,
                  #否则会把 /Source 到 # 之间的空格也算进去的。
                  #在下面引用该变量的时候实际上‘/Source_’最后还多了一个空格
                  #为了表示清楚,我用下划线表示空格,这样的目录肯定是不存在的

#以下语句调用了Shell函数,其结果是把指定目录下的所有源文件赋值给 x 变量,
#两者取其一运行make
#x := $(shell cd $(TmpDir); ls *.c)
x = $(shell cd $(TmpDir); ls *.c)

all:
    @echo $(x)
    @echo $(x)
    @echo $(x)
Copy after login
  1. := is an immediate variable assignment, the value has been determined when it is defined. When executing make, the value of = is a delayed variable assignment that only expands when the variable is used. When executing make, the value of x was not expanded when it was defined, so @echo $(x) was executed three times, and the Shell function was called each time. Finally, the Shell function was called three times
  2. Through the above analysis process, you should have a clear idea of ​​how these two operators affect the compilation speed.

Through this article, you should have a basic understanding of some performance optimization methods of Linux Makefile, and know how to improve compilation speed and efficiency, such as using parallel compilation, incremental compilation, precompiled header files, cached compilation results, etc. . You should also understand the principles and impact of Makefile performance optimization, and how to correctly use and configure these methods under Linux. We recommend that when using Makefile, you choose the appropriate performance optimization method based on your own project and needs to improve the build quality and speed of the program. At the same time, we also remind you to pay attention to some potential issues and challenges when using Makefiles, such as compatibility, stability, maintainability, etc. I hope this article can help you make better use of the Linux system and allow you to enjoy the benefits and convenience of Makefile performance optimization under Linux.

The above is the detailed content of Performance optimization of Linux Makefile: How to improve compilation speed and efficiency. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template