0
点赞
收藏
分享

微信扫一扫

el-menu 导航栏学习-for循环封装(2)

今天你读书了吗 2023-10-06 阅读 49
linuxcentos

一、 什么是make&Makefile ?

二、  实例代码

0x01 Makefile的编辑

[wh@localhost lesson7]$ touch test.c
[wh@localhost lesson7]$ touch Makefile //Makefile建议开头大写
[wh@localhost lesson7]$ vim test.c
[wh@localhost lesson7]$ vim Makefile
[wh@localhost lesson7]$ make
gcc test.c -o test -std=c99
[wh@localhost lesson7]$ ll
total 20
-rw-rw-r--. 1 wh wh   41 Oct  5 05:54 Makefile
-rwxrwxr-x. 1 wh wh 8512 Oct  5 05:54 test
-rw-rw-r--. 1 wh wh   73 Oct  5 05:54 test.c
[wh@localhost lesson7]$ ./test
hello world
[wh@localhost lesson7]$ cat Makefile
test:test.c    //test的形成依赖于test.c,也可以说目标文件依赖于依赖文件
	gcc test.c -o test -std=c99  //依赖方法
[wh@localhost lesson7]$ cat Makefile
test:test.c
	gcc test.c -o test -std=c99

.PHONY:clean
clean:
		rm -f test

0x02 上述代码中为什么只输入make就形成了可执行程序test,而必须输入make clean才能进行清理呢?

当然也可以用$@代替目标文件,$^代替源文件

  1 test:test.c
  2   //gcc test.c -o test -std=c99  
  3   gcc -o $@ $^   
  4 .PHONY:clean
  5 clean:
  6     rm -f test

上述的test:test.c依赖关系也可以细分为:

  1 test:test.o
  2   gcc  test.o -o test                                                                                        
  3 test.o:test.s
  4   gcc -c test.s -o test.o
  5 test.s:test.i
  6   gcc -S test.i -o test.s
  7 test.i:test.c
  8   gcc -E test.c -o test.i
  9 .PHONY:clean
 10 clean:
 11     rm -f test test.i test.s test.o

0x03 printf在sleep之前,为什么先进行6s之后,printf才输出呢?难道是sleep先于printf运行吗?

C程序,默认会打开三个流:

三、Linux 第一个小程序 --进度条

0x01 初步学习

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 
  4 int main()
  5 {
  6   int count = 10;                                                                                            
  7   while(count)
  8   {
  9     printf("%d\r",count);
 10     fflush(stdout);
 11     count--;
 12     sleep(1);
 13   }
 14   return 0;
 15 }

① 此时,显示器中显示的是10,90,80,70,...,10,这是为什么呢?
凡是从键盘读取的内容都是字符和显示到显示器上面的内容都是字符,比如pirntf("%d\n",234);每次显示的是字符2,字符3,字符4,每次%r,都是只重置到首字母,但是不换行,fflush刷新的都是一个字符
②那如何解决这个问题呢?
可以预留一部分空间出来,printf("%2d\r");留出来俩个空间

0x02 进一步编写

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<unistd.h>
  4                                                                                                              
  5 int main()
  6 {
  7 #define NUM 100
  8   char bar[NUM+1];//预留出'\0'的位置
  9   memset(bar,'\0',sizeof(bar));
 10 
 11   int i = 0;
 12   while(i <= 100)
 13   {
 14     printf("%s\r",bar);
 15     fflush(stdout);
 16     bar[i] = '#';
 17     i++;
 18     sleep(1);
 19   }
 20 }

0x03 增加[ ]和百分比和运行光标

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<unistd.h>
  4 
  5 int main()
  6 {
  7 #define NUM 100
  8   char bar[NUM+1];
  9   memset(bar,'\0',sizeof(bar));
 10   const char* label = '|/-\\'
 11   int i = 0;
 12   while(i <= 100)
 13   {
 14     printf("[%-100s][%d%%] %c\r",bar,i,label[i%4]);
        //预留100个空间,%是转义字符,%%表示一个%
        //因为默认格式控制是右对齐,所以是从右往左打印,加'-'则是左对齐,从左往右打印
 15     fflush(stdout);
 16     bar[i] = '#';
 17     i++;
 18     usleep(50000);
 19   }
 20   printf("\n");                                                                                              
 21 }

0x04 运行成果

举报

相关推荐

0 条评论