[Compile] 1. The first simple makefile example,

Source: Internet
Author: User

[Compile] 1. The first simple makefile example,

 

Preface

This article introduces makefile with the simplest example and teaches you to write the first makefile.

 

Body

The Download/aa folder contains a. c and makefile files.

1 litao@litao:~/Downloads/aa$ ls2 a.c  makefile

A. c:

 1 #include<stdio.h> 2 int main() 3 { 4   int i,j; 5   for(i=0;i<10;i++) 6   { 7         for(j=2*i+1;j>0;j--) 8         { 9                 printf("#");10         }11         printf("\n");12   }13 }

Makefile:

The format is:

1 a:. o # linK 2 gcc. o-o a 3. o:. s # compile ing 4 gcc-c. c-o. o 5. s:. I # compile 6 gcc-S. i-o. s 7. i:. c # pre-processing 8 gcc-E. c-o. I 9 10 11 run: 12. /a13 clear: 14 rm-rf. o. s. I

 

The basic unit of the makefile file isRules.One rule specifies one or more target files. The target file is followed by the commands used to compile and generate the files or modules on which the target file depends. The format is as follows:

The content in [] is optional.

The above 1st and 2 rows constitute a rule ~

Note that the command used to generate the target file must start with the Tab key (if a line starts with the tab key, make considers it a command)

Add a ";" sign after the list of dependent files, which can be followed by the command, as follows:

 

What are the ghosts of Lines 11 and 12?

Is a pseudo-target!

In makefile, the target file can be divided into two types: real targets and pseudo targets. The real target file is the target file that is actually generated and stored on the hard disk as a file. The pseudo-target does not need to generate the actual file. Instead, it allows make to execute some auxiliary commands, such as printing some information and deleting useless intermediate files. Here, run Runs to generate a program, clear is to clear all generated files ~

 

The effect is as follows:

 

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.