Getting started with embedded Linux (eleven, make and MakeFile)

嵌入式 Linux 入门第十一课,Make 工具和 Makefile 的引入...... 矜辰所致

foreword

It has been more than half a year since the last article of the Linux series, because various things have been delayed until now, many friends have asked many times, and have been waiting, thank you for your support! It's true that time has dragged on for too long, it's time to refuel!

Simply sort out the ideas, and pass through a simple article. The previous article talked about C programming under Linux. This article will introduce Make and Makefile.

❤️ 嵌入式 Linux 入门系列博文:
嵌入式 Linux 入门(一、Linux 基本介绍及文件结构)
嵌入式 Linux 入门(二、Linux 文件系统、文件类型及权限管理)
嵌入式 Linux 入门(三、Linux Shell 及常用命令说明)
嵌入式 Linux 入门(四、Linux 下的编辑器 — 让人爱恨交加的 vi )
嵌入式 Linux 入门(五、Shell 脚本编程上:认识 Shell 脚本)
嵌入式 Linux 入门(六、Shell 脚本编程下:Shell 脚本语法)
嵌入式 Linux 入门(七、Linux 下的环境变量)
嵌入式 Linux 入门(八、Linux 下的软件安装)
嵌入式 Linux 入门(九、Linux 下的磁盘管理)
嵌入式 Linux 入门(十、Linux 下的 C 编程)
.
嵌入式 Linux 环境搭建系列博文:嵌入式 Linux 环境篇
.
我是矜辰所致,全网同名,尽量用心写好每一系列文章,不浮夸,不将就,认真对待学知识的我们,矜辰所致,金石为开!

1. Multi-file compilation under Linux

In the last C programming under Linux, we know that the compiler under Linux is GCC, and how to use GCC to compile. In the article, we explain the compilation of GCC. We only use a .c file and use it directly

gcc [目标文件] -o [想要生产的文件名字]

So how to deal with multiple .c files?

In fact, the above instruction is still used, where there [目标文件]can be multiple.

To give a simple example, we wrote three .c files with the following contents:

insert image description here

The compilation method is as follows:

insert image description here

Doesn't it look so simple? But we can just imagine, when we do projects in the future, there are thousands of files, what should we do?

We have 2 main questions to consider:

1. How to deal with the increase in the number of files?

2. Only a single file has been modified, how to recompile only the modified single file?

It is impossible for us to manually use the gcc command to enter the file name like the above example, and there are too many files, and the compilation time will take a long time. Using the command in the above example will recompile all the files. In the later linux learning and development, the compilation time of the project is much longer than when learning 51 and STM32 projects. It is definitely not advisable to compile all files every time.

Under Windows, we have various integrated IDE tools, often only need a button to compile the whole project, which is convenient and quick, such as Keil we are familiar with, we only need to click a button in the IDE toolbar to complete the operation we want:

insert image description here

So how do we deal with it under Linux?

This will use the things mainly explained in this article: Make tool and Makefile.

2. Make tool and Makefile

First of all, we need to know what is the Make tool and what is a Makefile.

The content of this part is the basic concept, similar to the explanation of nouns, just remember it, there is nothing special to explain, because our focus is to know and learn how to use them.

2.1 What are make and Makefile?

make tool:

make is essentially a program, a compilation tool. When we proceed with the program later, we need to use the make command.

Using the make tool, we can automatically complete the compilation work, and can effectively deal with the problems we mentioned above:

  1. Automatic compilation of multiple files;
  2. Every time the make command is executed, only the modified file is recompiled;
    .Here
    is a word, how does Linux judge whether the file has been modified: the modification time of the file! It is through ls -lthe time of checking, make will recompile the dependent file according to the dependency relationship, if it finds that the modification time of the dependent file is newer than its own)

The use of the make tool can decompose a large-scale development project into multiple modules that are easier to manage, and can neatly sort out the complicated interrelationships between various source files, greatly simplifying the development work, and avoiding unnecessary recompilation.

The make tool completes and automatically maintains the compilation work through a file called Makefile, which describes the compilation and connection rules of the entire project.

Makefile file:

Makefile is a file that describes a series of rules.

The M at the beginning of the Makefile should be capitalized! (Although it is said in some places that both upper and lower case are acceptable, there will definitely be no inexplicable problems if you guarantee upper and lower case).

make will execute commands according to the rules of the Makefile, and finally complete the compilation output.

In the Makefile:

  • Define a series of rules to formulate the sequence of source files after compilation
  • Specific grammar rules, support function definition, function call
  • Can directly integrate commands in the operating system

This is a key object for us to learn Linux. For novices, at least they must be able to understand and modify the Makefile to a certain extent.

2.2 Get familiar with Makefile in advance through STM32

In fact, for the development of STM32, we can also use the Make tool and Makefile. If you are currently developing with STM32, you may wish to use the GCC toolchain for development, so that you can also be familiar with the Makefile in advance.

If you are a fan of mine, you must know that I use the gcc toolchain for STM32 now. For details, please refer to the article:

Use VScode to build an ARM development environment under the window

If you want to see a simple Makefile, you can write a simple STM32 project, and then select Makefile when STM32CubeMX generates the project, as shown below:

insert image description here

Then open the project, you can look at the Makefile automatically generated by the ST tool, and let yourself be familiar with the Makefile in advance:

insert image description here

2.3 What is the relationship/difference between GCC and make?

There are often friends who are confused by GCC and make tools. Although they know how to use them, they can't tell the difference or explain it.

See the concept clearly: GCC is a compiler, and make is a command tool.

I feel that the concept is clear when I say it. GCC is a compiler for compiling C language, and make is a tool that can simplify the compilation work, but when we use it, make will call the GCC compiler to complete the compilation work.

In the Makefile, we will specify the compiler type to execute the compilation, and will perform corresponding command operations according to the specified compiler type, as shown in the figure below:

insert image description here

Because we use the gcc compiler under linux, we will see the familiar gcc operation commands in the Makefile. We just use the automatic compilation of the make tool to call the gcc compiler to compile the project.

3. A simple Makefile

As mentioned above, Makefile is one of the main points of learning Linux. For the Makefile description, I will introduce it in a separate column. We have just come into contact with this article. Let us write a simple Makefile to compile the 3 files in our example at the beginning of the article.

Just write a fool-like one first to see if the make tool is useful. In the same directory as the example.c file, we create a new Makefile, as follows:

insert image description here

Then try to use the make command to see the effect:

insert image description here

OK, sure enough, make can be used to compile directly, but the Makefile above is problematic, because every time make is executed, all .c files will be recompiled once, and it is impossible to write this way in actual use.

Let's recall the content of the previous course. A C file must be preprocessed, compiled, assembled, and linked before it can be turned into an executable file. Different files are generated into different files, and finally all files are .clinked .ointo .oexecutable files.

.cSo we can do it separately, generate each .ofile first, and then .ogenerate the final file from all the files, so that if a file is modified separately, only the modified .cfile needs to be recompiled, so our Makefile should be written like this:
insert image description here

The result of make is as follows:

insert image description here

The advantage of writing this way is that if we only modify a certain .cfile, it will only recompile the modified file. We can test it, for example, if we modify a.cthe file:

insert image description here

With the simple Makefile above, can you already feel the convenience and speed of the make tool?

epilogue

This article introduces the make tool used in Linux programming. How to compile projects efficiently and quickly in a Linux environment without IDE tools depends on the make tool.

Of course, our focus is on the Makefile. I will write a separate article explaining more about the writing rules of the Makefile.

For beginners, there is no need to do it overnight. It is important to write a good Makefile, but for general application development now, it is not often necessary to write a Makefile from scratch. In many cases, you can find an available template for modification. We must at least make it understandable and modify the Makefile!

Well, that's all for this article, thank you all!

Guess you like

Origin blog.csdn.net/weixin_42328389/article/details/127325913