What exactly does target in makefile represent ?, Makefiletarget

Source: Internet
Author: User

What exactly does target in makefile represent ?, Makefiletarget

When I first started using make, I thought that the target named main in makefile is the target executed by make by default (the Chinese translation is called the target (di, four voices), and the following is called the target ), then I was naive enough to make such a mistake. c file (Name: main. c) The function is to print helloworld, And then I write this makefile for compilation:

main:    gcc -o main main.c

The result can be compiled using the make command for the first time. If you want to use the make command for later compilation, the following error will occur:

  

This error occurs even if the code in main. c is changed. At that time, I had never figured out why the first compilation would result in an error after compilation. Later I learned the makefile syntax to know that makefile is defined in this way.

target ...: prerequisites            command            command

Target represents a subject, and prerequisites represents the subject that the subject depends on. The following command is the command to generate the subject.

So what does that mark represent? In fact, it represents a file with the same name as it, which is generated after compilation. o file. The executable file generated after the link is compiled or any other types of files. For example, the main mark indicates the main executable file.

 

This may be a bit obscure. Let's take the above error as an example. In the above example, after executing make, it does the following:

1. Search for makefile. By default, the first mark (main) is executed ).

2. Check whether the file main does not exist or you do not need to update it.

3. If the main file does not exist, run the gcc command below to generate the file.

4. If the main file already exists and its modification time is earlier than the modification time of the dependent file (that is, the main file has expired), execute the following gcc command to generate the file.

After reading the above steps, you should be able to understand why I reported an error. It is because the file on which my main object depends is empty that the main file will never expire, therefore, every time you execute the make command, the system will prompt that main is up to date. (This file is the latest and does not need to be updated)

So what should we do? We can change it to this form:

main: main.c    gcc -o main main.c

Here, a dependency is added for the main mark, depending on main. c file, if main. c. The modification time of this file is later than the modification time of the main file. Execute the following gcc command to generate the main file.

 

OK. After this analysis, let's analyze another classic Mark, "clean", or the preceding Chestnut. I added the "clean" mark to delete the executable file of the generated main, as follows:

main: main.c    gcc -o main main.cclean:    rm main

If I run the make clean command, the executable file main will be deleted. What is the principle of this mark? In fact, after we run the make clean command, its execution steps are as follows:

Because the clean mark does not depend on the file, it is to check whether the clean file exists. If it does not exist, execute the command below the clean mark to generate the clean file. However, the following rm command does not generate the clean file, so the result is that every time we run the make clean command, the following rm command will be executed. As shown in:

  

So what if we add a clean file in the current directory? The following results will be displayed:

  

This time, the commands below the clean mark will not be executed.

  

OK. Here is the content of the makefile that I want to talk about.

1. Each mark in makefile represents a file.

2. If you only run the make command, the first mark is executed by default.

3. When you execute a tag, two things will be done:

3. 1. check whether a file with the same name as this object exists. If it does not exist, run the following command for this object.

3. 2. if a file with the same name as the object already exists, check whether the file with the same name as the object needs to be updated (that is, whether the modification time of the object is earlier than the modification time of the dependent object ), if an update is required, run the following command.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.