Implementation of make, makefile use and progress bar in Liunx

Source: Internet
Author: User
Tags usleep
Make  Makefile

here, I would like to say more about the procedures of compiling some of the norms and methods, in general, whether C, C + +, or PAS, the first to compile the source file into the intermediate code file, under Windows is the. obj file, Unix is the. o file, that is, Object file, this action Do compile (compile). And then a lot of objectfile to the execution file, this action is called Link pas, first to compile the source file into the intermediate code file, under Windows is the. obj file, Unix is an. o file, that is, Object file, This action is called compilation (compile). Then a lot of objectfile are synthesized to execute the file, this action is called link.

At compile time, the compiler needs the correct syntax, the declaration of functions and variables. For the latter, it is usually the location where you need to tell the compiler header file.

Links are mainly linked functions and global variables, so we can use these intermediate target files (o files or obj files) to link our applications.

I. Introduction to make and makefile

Make: is a very important compilation command, which is essentially a program. With make tools, you can break down large development projects into more manageable modules, and for an application that includes hundreds of source files, using make and makefile tools makes it easy to streamline the intricacies of each source file. And so many source files, if you have to type the GCC command to compile each time, it would be a disaster for programmers. The Make tool automates compilation and can compile only those parts of the programmer that have been modified since the last compilation.

Make is an order;

Makefile is a document;

When the make command executes, a Makefile⽂ file is required to tell the make command how to compile and link the program.

Makefile to tell the make command how to compile and link the ⼏ several ⽂ files.            Our rule is: 1. If the project has not been compiled, then all of our C files will be compiled and linked.            2. If a few C files of this project are modified, we only compile the modified C file and link the target program. 3. If the header file for this project has been changed, we need to compile the C file referencing the header files and link the target program.

After defining the dependencies, the subsequent line defines how to generate the operating system commands for the target file, and must begin with a TAB key. Remember that make does not matter how the command works, but he executes the commands that are defined.

Build two files Main.c test.c.

The MAIN.C code is as follows

1 #include <stdio.h>
  2 void my_printf ();
  3 int main ()
  4 {
  5  my_printf ();
  6 return  0;
  7}
The TEST.C code is as follows

1 #include <stdio.h>
  2 void my_printf ()
  3 {
  4   printf ("hello\n");
  5}
~                  

Makefile file

In this case, the pseudo target can be set, and you want to delete it.

When you are done, enter the command: make (generate intermediate files based on dependency rules) after completion (view) LS


Make clean clear intermediate files


Two, how make how to work

1, when we only enter the Make command (by default) makes will look for "makefile" or "makefile" files in the current directory.

2. If found, it will find the first target file in the file.

3. If the first file does not exist or the first file depends on the file modification time of the later. o file, then he executes the following file to generate the file.

4. If the. o file that the first file depends on does not exist, make will find a dependent file in the current file that targets the. o file and generate the. o file based on that dependency rule if found

5, the final use of the. o file, the implementation of the first file.

The entire make dependency, he will be based on a layer of dependencies to find, until the final compilation of the first target file, if there is an error in the lookup process then make will exit directly, and error. For a defined command error, or if the compilation is unsuccessful, make simply ignore it. Make just ⽂ the dependencies of the file, that is, if after I find a dependency, the ⾯ file is not there after the colon, then it stops.

III. implementation of Linux terminal progress bar

Principle:

1 sets a character array to hold the output string

2 use \ r carriage return so that each time the cursor back to the leftmost, overwriting the previous string (here \ r is a carriage return generated overlay effect is not a newline so there will be no output in the next line, only on the original basis of the change)

3 does not use Fflush Refresh (stdout force output from output buffer to screen)

4 fixed output length with%-101s so that it is left-aligned

for \ R and \ n we want to talk about the difference between them:

For \ R, it is a carriage return, in order to move to the next row header. (Outputs output to an output buffer)

For \ n, it is a newline, which is to move the cursor down one line. (Outputs the output buffer to the screen)

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <unistd.h>
  4 
  5 void Progress ()
  6 {
  7         char arr[103];
  8         char *index= "|/-\\0";
  9         int i=0;         memset (arr, ' the ", sizeof (arr));
 One by one         arr[0]= ' [';         arr[101]= '] ';         "arr[102]=";
 For         (i;i<=100;++i)         {                 arr[i+1]= ' = ';                 printf ("%-101s[%d%%][%c]\r", arr,i,index[i%4]);                 fflush (stdout);                 Usleep (MB);         printf ("\ n");
In the above printing process, the fixed output length with%-101s is left-aligned.
The above code structure



Here, we use procedures to measure the following \

{\ n will force the result to be displayed from the output buffer to the monitor if not \ n The result is output to the output buffer before the output buffer is full or the program is run to the output monitor}

#include <stdio.h>
int main ()
{
    printf ("%10s\n", "I am Student");
    Usleep ();
  return 0;
}


In this case, first print out I am student, then wait for 200US;

#include <stdio.h>
int main ()
{
    printf ("%10s", "I am Student");
    Usleep ();
  return 0;
}


After removing \ n, wait for 200us first and then output the I am student to the screen.

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.