Teach you how to write makefile files

Source: Internet
Author: User

A:I. makefile rules
Before talking about this makefile, let's take a rough look at the makefile rules.

Target...: prerequisites...
Command
...

Target is a target file, which can be an object file or an execution file. It can also be a label. For the label feature, it will be described in the subsequent "pseudo-target" chapter.

Prerequisites is the file or target required to generate the target.

Command is the command to be executed by make. (Any shell command)

This is the dependency of a file. That is to say, one or more target files depend on the files in prerequisites, and their generation rules are defined in command. To put it bluntly, if more than one prerequisites file is newer than the target file, the command defined by command will be executed. This is the makefile rule. That is, the core content in makefile.

In the end, makefile is like this, as if my document should be over. Haha. This is the main line and core of makefile, but it is not enough to write a makefile. I will give you some experience in the future. There are plenty of contents. :)

 

Ii. Example
As mentioned above, if a project has three header files and eight C files, we want to complete the three rules described above, our makefile should look like the following.

Edit: Main. o kbd. O command. O display. O insert. O search. O files. O utils. o
CC-O edit main. o kbd. O command. O display. O insert. O search. O files. O utils. o

Main. O: Main. c defs. h
CC-C main. c
KBD. O: KBD. c defs. H command. h
CC-c kbd. c
Command. O: Command. c defs. H command. h
CC-C command. c
Display. O: Display. c defs. h buffer. h
CC-C display. c
Insert. O: insert. c defs. h buffer. h
CC-C insert. c
Search. O: search. c defs. h buffer. h
CC-C search. c
Files. O: files. c defs. h buffer. H command. h
CC-C files. c
Utils. O: utils. c defs. h
CC-C utils. c
Clean:
Rm edit main. o kbd. O command. O display. O insert. O search. O files. O utils. o

The backslash (\) indicates a line break. This makes it easier to read makefile. We can save this content in a file named "makefile" or "makefile", and then directly enter the command "make" in this directory to generate the execution file edit. If you want to delete the execution file and all the intermediate target files, simply execute "make clean.

In this makefile, the target file contains the execution file edit and intermediate target file (*. o), the dependent files (Prerequisites) are the ones after the colon. c file and. h file. Every

Related Article

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.