Simple use of make command in linux and writing of Makefile

Makefile

   

    Whether or not to write makele shows from one side whether one has the ability to complete large-scale projects.

    The source files in a project are not counted, they are placed in several directories according to type, function and module. Makele defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files need to be recompiled. Compile, and even perform more complex functional operations, because makele is like a shell script, which can also execute the commands of the operating system. The advantage of makele is "automatic compilation". Once written, only one make command is needed, and the entire project is completely automatically compiled, which greatly improves the efficiency of software development. make is a command tool, a command tool that interprets the instructions in makele, and makele has become a compilation method in engineering.

 

1. Compilation and linking of programs

    Generally speaking, whether it is C , C++ , or pas , the source file must first be compiled into an intermediate code file, which is an .obj file under Windows and an .o file, which is an Object File under UNIX . This action is called compilation ( compile ). Then a large number of Object Files are synthesized into executable files, which is called a link .

 

    When the make command is executed, a Make le file is required to tell the make command how to compile and link the program.

 

    Make le writing rules:

    1. If the project has not been compiled, then all our C files must be compiled and linked.           

    2. If some C files of this project are modified, then we only compile the modified C files and link the target program.           

3. If the header file of this project has been changed, then we need to compile the C file that     references these header files and link the target program.

 

    After defining the dependencies, the following line defines how to generate the operating system command of the object file, which must start with a Tab key ( make doesn't care how the command works, he just executes the defined command.).

 

2. How Make Works

    1. make will look for a file named " Make le " or " make le " in the current directory;

    2. If found, it will look for the first object file .o in the file ;

3. If the file does not exist, it will look for the .s file     according to the dependencies ;

    4. If the .s file does not exist, make will look for the dependent file .i of the .s file in the current file;    

    5、若.i文件不存在,会根据依赖关系查找.c文件,当然.c文件是存在的,于是,make会生成一个.o文件,然后去执行文件。

 

    这就是整个make的依赖性,make会一层又一层地去找文件的依赖关系,直到最终编译出第一个目标文件。在找寻的过程中,如果出现错误,比如最后被依赖的文件找不到,那么make就会直接退出,并报错,而对于所定义的命令的错误,或是编译不成功,make根本不理。make只管文件的依赖性,即如果在我找了依赖关系之后,冒号后面的文件还是不在,那么make就不工作啦。

  

三、清空目标文件的规则

    每个Makele中都应该写个清空目标文件(.o和执行文件)的规则,这不仅便于重编译,也很利于保持文件的清洁。

    一般格式:

 .PHONY : clean        

 clean :               

 -rm -f 文件

    .PHONY意思表示clean是个“伪目标”,而在rm命令前面加了一个小减号的意思就是:也许某些文件出现问题,但不要管,继续做后面的事。当然,clean的规则不要放在文件的开头,不然,就会变成make的默认目标。

 

    例如为创建一个进度条(后边详细讲解)的程序(test.c)编写一个Makefile文件,并在Makefile文件下编辑如下:

    格式1:

wKioL1dKnwOA9QS7AAAg5YLxgCU007.png-wh_50

    (注意:如果按格式1写Makefile文件,需要将每行开头处的‘#’删除掉,此为注释符)

    格式2:

wKiom1dKnifSSkrnAAAXESlqQAU392.png-wh_50

 

    运行该文件效果如下:

wKioL1dKn2zBtOSXAAAYvmLWsbQ128.png-wh_50

    清除Makefile产生的多余文件make  clean

wKiom1dKnpuy2SaeAAAZ7XZuTRo612.png-wh_50

    注:   

    1、编写依赖方式时,一定要以Tab键开头(不能用空格代替)

    2、clean一般都放在文件的最后

    3、@可以屏蔽输出依赖方式

    4、makefile 默认值生成一个目标文件,就停止

    

 

   linux下编写一个简单的进度条

 

    进度条实现原理:

1)实现动态的增长

    定义一个数组,并设置一个计数器,当数字增加的同时,给字符数组每次增加一个元素,并且打印时覆盖之前的数组。

2)保证打印信息输出在一行

    每次将光标定位在最左边,从标处打印,使用‘\n’,回车

3)复制动态的效果实现

    定义一个数组

     char *index = -/|\\;

    输出时index[count%4];

4)实现打印长度相同

    %-100s

5)给人复制的效果

    程序每输出一次,程序挂起一段时间

 

     代码如下:

wKiom1dKn_LQ7MphAABwFz0ma6g554.png-wh_50

    

    运行效果如下:

 

wKiom1dKoBiA1p7kAAAkRO48UFA170.png-wh_50

wKiom1dKoBjSnKLhAAAo-QrUwQ0669.png-wh_50

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326102166&siteId=291194637