[Linux learning] Makefile Learning (2)

Source: Internet
Author: User

If there is no IDE like windows, how does one compile a project file? For example, how can I run a windows project file under Linux? Next we will first understand two concepts: Compilation and linking.

When we get used to programming in windows, after writing the program, we only need to click the button to execute and generate executable files. We will certainly think it is like this: as long as we write the program correctly and then click the button. In fact, the IDE in windows hides "Compilation" and "link" for us ". Whether it is C or C ++, you must first compile the source file into an intermediate code file. in Linux, It is a file with the suffix ". o". This action is called compilation. The link is to combine these intermediate code files into executable files.

In Linux, when running a project file, Makefile is used to compile the connection function to generate an executable file. After we write the Makefile and run make, an executable file is generated. In Makefile, a series of rules are defined to specify which files are first compiled, which files are then compiled, and so on. If you have written the correct Makefile in a project file, you may wonder why the executable file will appear after you execute make. What is the working principle of Make?

In fact, after entering make, make first finds the Makefile or makefile file in the current directory, and finds makefie. Then, it finds the first target file ABC_1 (which we assume) in the file ), this file will also be used as the final target file. If ABC_1 does not exist or the modification time of the ABC_2.o file dependent on ABC_1 is later than ABC_1, the command defined later will be executed to generate ABC_1. However, if the ABC_2.o file on which ABC_1 depends does not exist, make will find the file ABC_3.o on which ABC_2.o is generated in this directory, and then generate ABC_2.o according to the generation rules, in this way, we want to find the file dependency in a recursive way until we find the target file that was first generated.

The following are two programs written when learning Makefile:

Source program file 1:


Makefile file:


Running result:


The source file of the first program is a simple printf () function. "hello" in makefile is the final target file to be generated, and "hello" is the file to be depended on after the colon, the command needed to generate hello is later. The following "clean" is not associated with the first target file, so the command defined by it will not be automatically executed. Therefore, to clear the files generated during program execution, enter make clean.

Source program file 2:



Makefile file:


Running result:


In the second program, there are two source files. Helloworld in makefile is the final target file to be generated. Then make will find the main dependency file of Helloworld. o. In this case, main. o does not exist. make will continue to find and generate main. o rules, generate main. o, then make will find the Helloworld dependent file. o,. o does not exist, and then make finds the generated. o rules, generate. o, and finally generate Helloworld



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.