Simple use of make and makefile from a small program

Source: Internet
Author: User

We all know that there are two important processes before the program executes: Compile and link. Of course we are in the Windows platform compiler (like VS), it will automatically help us to generate the final execution program, such as (Xxx.exe), but Linux under the use of Vim, GCC and g++ can be a one-time to generate a source code executable program, However, a project can have more than one source file, and the operation of a project is to compile the individual source code into the target file, and then link them up after the execution.

Imagine if a project has hundreds of source files, then you will all the source files are compiled link, the program can also run up, and then your boss is not satisfied, let you modify a few places, and then you have to write again after the end of the very hard to write, he is not happy, and then you have to write again ... In fact, most of the time here you are wasted on the useless, and those who compile and link the command as long as it is good to write once, every time after the change, just tell them to run it again.

While make and makefile can solve this problem, you write your compile and link commands to makefile, and execute the make command every time you run it, it will generate your execution program. If you want to change it, then you just have to modify it after the execution of the make command, is it convenient? (hehe), actually learned to make and makefile use of your programming to learn the efficiency and efficiency are very helpful, below to see how they use it:

I have the progress of this small program, for example, about the progress bar is certainly not unfamiliar, as long as the network has been seen, its code is very simple, I let it every time a specified, refresh a ' # ', followed by a percentage. Refresh only in the current line, so each time to enter, the code is as follows:

#include <stdio.h>  #include <string.h>   void proc ()   {       char str[102];      char s[]={' | ', '/', '- ', ' \ \ '};      memset (str, ' + ', sizeof (str));      int  count=100;      int i=0;     for (; i<= Count;++i)       {         str[i]= ' # ';           printf ("[%-100s][%d%%][%c]\r", str,i,s[i%4]);           usleep (100000);           fflush (stdout);      }       printf ("\ n");       return;   }    int main ()   {     proc ();      return 0;   } 

Results because it is dynamic, here is not good hair map, do not show, we still look at the back of the makefile how to write it, this is the focus.

Above is a well-written source file called PROC.C, which generates the application proc to go through the following procedure:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/2F/wKiom1dNfibydcuGAAANbBgUM_0565.png "title=" Proc.png "alt=" Wkiom1dnfibydcugaaanbbgum_0565.png "/>

Makefile to write two points: dependencies and Dependency methods

As far as the above procedure, we finally want to generate proc program, Proc to be generated through PROC.O, but now there is no proc.o, so in the future to see Proc, O is generated by Proc.s, and then pushed down will find we pushed to PEOC.C, because PROC.C is there, so it can generate PROC.I, and then push up, and finally can generate proc this program.

This is the entire program dependencies, and the dependency method is the command we use, the following is the proc.c of the makefile writing:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/2D/wKioL1dNg3yDRnQUAAAxHIiXmkM627.png "title=" Make.png "alt=" Wkiol1dng3ydrnquaaaxhiixmkm627.png "/>

First the dependency, then the dependency method, write the dependency when the target file is in front, the dependent file is in the back

Another thing to be reminded of is that when writing a dependency method, the Tab key is used before

There is an unwritten rule in MAKEFILR:

The ability to empty the target file in every makefile

After all, the things you write yourself to be responsible for it, if you have, you have to change the program after the removal of the previous procedure, so bad management, sometimes will pit to others, so to have this "culture"!

The way to clean up the target file is to use it first. Phony create a "pseudo-target", I above the clean is a pseudo-target, and then to the pseudo-target definition method, that is, delete the target file command.

There are rules Claer must be placed at the end of the document!

The next step is to execute our make command to see the effect, and here's how make works, huh:

When make executes, it will look for the makefile file in the current directory, then find the first target file, take it as the final file, and generate the final file by the dependency method you gave, then stop.

Since our clean and final files do not have a half-dime relationship, we want to perform clean, which will only work if the make command is used to display the call.

Let's look at the effect of make in the example above:

First Execute make

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/2F/wKiom1dNi5qR2OHXAABeRyFmvyE563.png "title=" make " 1.png "alt=" Wkiom1dni5qr2ohxaaberyfmvye563.png "/>

At this point, Proc appears, indicating that the file was successfully compiled and linked.

We then use the make display to execute the clean:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/2F/wKiom1dNjHrgXeHzAAA8KydsH8A702.png "title=" Make2.png "alt=" Wkiom1dnjhrgxehzaaa8kydsh8a702.png "/>

And the proc program disappears.

This is make and makefile basic usage, or is very convenient, I hope you have seen after reading to you have some help, before saying goodbye, supplemented if you want to display the method of dependence in the terminal, the front of the @ removed Oh!

All right, here's another one!

This article is from the "waitting" blog, make sure to keep this source http://cuiyuxuan.blog.51cto.com/10786939/1784995

Simple use of make and makefile from a small program

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.