文章目录
4. 自动化构建-make/Makefile

[gsm@VM-4-3-centos lesson7]$ touch proc.c [gsm@VM-4-3-centos lesson7]$ ll total 0-rw-rw-r--1 gsm gsm 0 Oct 1215:53 proc.c [gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ ll total 4-rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 16-rwxrwxr-x 1 gsm gsm 8360 Oct 1215:55 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ rm proc [gsm@VM-4-3-centos lesson7]$ ll total 4-rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ touch Makefile [gsm@VM-4-3-centos lesson7]$ ll total 4-rw-rw-r--1 gsm gsm 0 Oct 1215:56 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 32 Oct 1215:58 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 32 Oct 1215:58 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1215:59 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ rm proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 32 Oct 1215:58 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 64 Oct 1216:14 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c gcc -o proc proc.c .PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 64 Oct 1216:14 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:15 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 64 Oct 1216:14 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ make proc gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 64 Oct 1216:14 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:22 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 64 Oct 1216:14 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile .PHONY:clean clean: rm -f proc proc:proc.c gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make rm -f proc [gsm@VM-4-3-centos lesson7]$ make proc gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 64 Oct 1216:24 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:24 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 94 Oct 1216:27 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:24 proc -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ rm proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 94 Oct 1216:27 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c echo "hahaha"#gcc-o proc proc.c.PHONY:clean clean: echo "hehe"#rm-f proc[gsm@VM-4-3-centos lesson7]$ make echo "hahaha" hahaha #gcc-o proc proc.c[gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c echo "hahaha".PHONY:clean clean: echo "hehe"#rm-f proc[gsm@VM-4-3-centos lesson7]$ make echo "hahaha" hahaha [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 73 Oct 1216:29 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1215:54 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ make echo "hahaha" hahaha echo "hahaha" hahaha echo "hahaha" hahaha echo "hahaha" hahaha [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c @echo "hahaha" @echo "hahaha" @echo "hahaha" @echo "hahaha".PHONY:clean clean: echo "hehe"#rm-f proc[gsm@VM-4-3-centos lesson7]$ make hahaha hahaha hahaha hahaha [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c @echo "开始编译..." @gcc -o proc proc.c @echo "结束编译...".PHONY:clean clean: @echo "清理完成..." @rm -f proc [gsm@VM-4-3-centos lesson7]$ make 开始编译... 结束编译...[gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>intmain(){printf("hello Linux!\n")return0;}[gsm@VM-4-3-centos lesson7]$ make clean 清理完成...[gsm@VM-4-3-centos lesson7]$ make 开始编译... proc.c: In function ‘main’: proc.c:6:5: error: expected ‘;’ before ‘return’ return0;^ make:***[proc] Error 1[gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 141 Oct 1216:35 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ make 开始编译... 结束编译...[gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 141 Oct 1216:35 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:39 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ make clean 清理完成...[gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 141 Oct 1216:35 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ cp Makefile backup1012 [gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 141 Oct 1216:41 backup1012 -rw-rw-r--1 gsm gsm 141 Oct 1216:35 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c gcc -o proc proc.c .PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 24-rw-rw-r--1 gsm gsm 141 Oct 1216:41 backup1012 -rw-rw-r--1 gsm gsm 64 Oct 1216:42 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:42 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ mv backup1012 ..[gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 64 Oct 1216:42 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:42 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 64 Oct 1216:42 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 64 Oct 1216:48 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1216:49 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c gcc -o proc proc.c #.PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile .PHONY:proc proc:proc.c gcc -o proc proc.c #.PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 78 Oct 1217:00 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:01 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 78 Oct 1217:00 Makefile -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile #.PHONY:proc proc:proc.c gcc -o proc proc.c #.PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:06 proc -rw-rw-r--1 gsm gsm 79 Oct 1216:39 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>intmain(){printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");return0;}[gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:07 proc -rw-rw-r--1 gsm gsm 169 Oct 1217:07 proc.c [gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:169 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0664/-rw-rw-r--) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:07:11.741037892+0800 Modify:2025-10-1217:07:11.739037901+0800 Change:2025-10-1217:07:11.739037901+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:07 proc -rw-rw-r--1 gsm gsm 169 Oct 1217:07 proc.c [gsm@VM-4-3-centos lesson7]$ chmod o-r proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:07 proc -rw-rw----1 gsm gsm 169 Oct 1217:07 proc.c [gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:169 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0660/-rw-rw----) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:07:11.741037892+0800 Modify:2025-10-1217:07:11.739037901+0800 Change:2025-10-1217:14:13.525097070+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>intmain(){printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");return0;}[gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:259 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0660/-rw-rw----) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:15:26.229763190+0800 Modify:2025-10-1217:15:26.226763204+0800 Change:2025-10-1217:15:26.226763204+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:07 proc -rw-rw----1 gsm gsm 259 Oct 1217:15 proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:259 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0660/-rw-rw----) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:15:26.229763190+0800 Modify:2025-10-1217:15:26.226763204+0800 Change:2025-10-1217:15:26.226763204+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ stat proc File: ‘proc’ Size:8360 Blocks:24 IO Block:4096 regular file Device: fd01h/64769d Inode:1052395 Links:1 Access:(0775/-rwxrwxr-x) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:23:44.872478158+0800 Modify:2025-10-1217:23:44.147481475+0800 Change:2025-10-1217:23:44.147481475+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ touch -m proc.c [gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:259 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0660/-rw-rw----) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:25:46.871920321+0800 Modify:2025-10-1217:25:45.957924498+0800 Change:2025-10-1217:25:45.957924498+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ stat proc File: ‘proc’ Size:8360 Blocks:24 IO Block:4096 regular file Device: fd01h/64769d Inode:1052395 Links:1 Access:(0775/-rwxrwxr-x) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:26:32.865710134+0800 Modify:2025-10-1217:26:32.422712158+0800 Change:2025-10-1217:26:32.422712158+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ make make: `proc' is up to date.[gsm@VM-4-3-centos lesson7]$ touch proc.c [gsm@VM-4-3-centos lesson7]$ stat proc.c File: ‘proc.c’ Size:259 Blocks:8 IO Block:4096 regular file Device: fd01h/64769d Inode:1052393 Links:1 Access:(0660/-rw-rw----) Uid:(1001/ gsm) Gid:(1001/ gsm) Access:2025-10-1217:27:22.865481713+0800 Modify:2025-10-1217:27:21.185489386+0800 Change:2025-10-1217:27:21.185489386+0800 Birth:-[gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 79 Oct 1217:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1217:27 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile .PHONY:proc proc:proc.c gcc -o proc proc.c #.PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -o proc proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 78 Oct 1217:30 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.c gcc -o proc proc.c .PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 65 Oct 1217:56 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ cp Makefile backup20251012_2 [gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 65 Oct 1217:57 backup20251012_2 -rw-rw-r--1 gsm gsm 65 Oct 1217:56 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ mv backup20251012_2 ..[gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 65 Oct 1217:56 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.o gcc proc.o -o proc proc.o:proc.s gcc -c proc.s -o proc.o proc.s:proc.i gcc -S proc.i -o proc.s proc.i:proc.c gcc -E proc.c -o proc.i .PHONY:clean clean: rm -f proc [gsm@VM-4-3-centos lesson7]$ make gcc -E proc.c -o proc.i gcc -S proc.i -o proc.s gcc -c proc.s -o proc.o gcc proc.o -o proc [gsm@VM-4-3-centos lesson7]$ ll total 48-rw-rw-r--1 gsm gsm 183 Oct 1218:05 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1218:05 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c -rw-rw-r--1 gsm gsm 17057 Oct 1218:05 proc.i -rw-rw-r--1 gsm gsm 1848 Oct 1218:05 proc.o -rw-rw-r--1 gsm gsm 622 Oct 1218:05 proc.s [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux![gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.o gcc proc.o -o proc proc.o:proc.s gcc -c proc.s -o proc.o proc.s:proc.i gcc -S proc.i -o proc.s proc.i:proc.c gcc -E proc.c -o proc.i .PHONY:clean clean: rm -f proc.i proc.s proc.o proc [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc.i proc.s proc.o proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 204 Oct 1218:07 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.o gcc proc.o -o proc proc.o:proc.c gcc -c proc.c .PHONY:clean clean: rm -f proc.o proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 102 Oct 1218:17 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ make gcc -c proc.c gcc proc.o -o proc [gsm@VM-4-3-centos lesson7]$ ll total 24-rw-rw-r--1 gsm gsm 102 Oct 1218:17 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1218:17 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c -rw-rw-r--1 gsm gsm 1848 Oct 1218:17 proc.o [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc.o proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 102 Oct 1218:17 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile proc:proc.o gcc proc.o -o proc %.o:%.c gcc -c $<.PHONY:clean clean: rm -f proc.o proc [gsm@VM-4-3-centos lesson7]$ make gcc -c proc.c gcc proc.o -o proc [gsm@VM-4-3-centos lesson7]$ ll total 24-rw-rw-r--1 gsm gsm 92 Oct 1218:20 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1218:22 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c -rw-rw-r--1 gsm gsm 1848 Oct 1218:22 proc.o [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc.o proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 92 Oct 1218:20 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile bin=proc src=proc.o $(bin):$(src) gcc $^-o $@ %.o:%.c gcc -c $<.PHONY:clean clean: rm -f proc.o $(bin)[gsm@VM-4-3-centos lesson7]$ make gcc -c proc.c gcc proc.o -o proc [gsm@VM-4-3-centos lesson7]$ ll total 24-rw-rw-r--1 gsm gsm 111 Oct 1219:47 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1219:47 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c -rw-rw-r--1 gsm gsm 1848 Oct 1219:47 proc.o [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux![gsm@VM-4-3-centos lesson7]$ make clean rm -f proc.o proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 111 Oct 1219:47 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ cat Makefile bin=hello src=proc.o $(bin):$(src) gcc $^-o $@ %.o:%.c gcc -c $<.PHONY:clean clean: rm -f proc.o $(bin)[gsm@VM-4-3-centos lesson7]$ make gcc -c proc.c gcc proc.o -o hello [gsm@VM-4-3-centos lesson7]$ ll total 24-rwxrwxr-x 1 gsm gsm 8360 Oct 1219:48 hello -rw-rw-r--1 gsm gsm 112 Oct 1219:48 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c -rw-rw-r--1 gsm gsm 1848 Oct 1219:48 proc.o [gsm@VM-4-3-centos lesson7]$ ./hello hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux![gsm@VM-4-3-centos lesson7]$ make clean rm -f proc.o hello [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 112 Oct 1219:48 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ cp Makefile backup20251012_2 [gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 112 Oct 1219:50 backup20251012_2 -rw-rw-r--1 gsm gsm 112 Oct 1219:48 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ mv backup20251012_2 ..[gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 112 Oct 1219:48 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile bin=proc src=proc.c $(bin):$(src) gcc $^-o $@ .PHONY:clean clean: rm -f $(bin)[gsm@VM-4-3-centos lesson7]$ make gcc proc.c -o proc [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 84 Oct 1220:06 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1220:07 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ make clean rm -f proc [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 84 Oct 1220:06 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ touch code.c [gsm@VM-4-3-centos lesson7]$ ll total 8-rw-rw-r--1 gsm gsm 0 Oct 1220:13 code.c -rw-rw-r--1 gsm gsm 84 Oct 1220:06 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c > code.c [gsm@VM-4-3-centos lesson7]$ vim code.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>intmain(){printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");printf("hello Linux!\n");return0;}[gsm@VM-4-3-centos lesson7]$ cat code.c #include<stdio.h>intmain(){printf("hello code!\n");printf("hello code!\n");printf("hello code!\n");printf("hello code!\n");printf("hello code!\n");printf("hello code!\n");return0;}[gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 84 Oct 1220:06 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile bin1=proc src1=proc.c bin2=code src2=code.c .PHONY:all all:$(bin1) $(bin2) $(bin1):$(src1) gcc $^-o $@ $(bin2):$(src2) gcc $^-o $@ .PHONY:clean clean: rm -f $(bin1) $(bin2)[gsm@VM-4-3-centos lesson7]$ make gcc proc.c -o proc gcc code.c -o code [gsm@VM-4-3-centos lesson7]$ ll total 36-rwxrwxr-x 1 gsm gsm 8360 Oct 1220:28 code -rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 184 Oct 1220:28 Makefile -rwxrwxr-x 1 gsm gsm 8360 Oct 1220:28 proc -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux! hello Linux![gsm@VM-4-3-centos lesson7]$ ./code hello code! hello code! hello code! hello code! hello code! hello code![gsm@VM-4-3-centos lesson7]$ make clean rm -f proc code [gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 184 Oct 1220:28 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c
5. Linux第一个系统程序−进度条

[gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 184 Oct 1220:28 Makefile -rw-rw----1 gsm gsm 259 Oct 1217:27 proc.c [gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>#include<unistd.h>intmain(){printf("hello Linux!\n");sleep(2);return0;}[gsm@VM-4-3-centos lesson7]$ man 3 sleep [gsm@VM-4-3-centos lesson7]$ vim Makefile [gsm@VM-4-3-centos lesson7]$ cat Makefile bin1=proc src1=proc.c #bin2=code#src2=code.c #.PHONY:all #all:$(bin1) $(bin2) $(bin1):$(src1) gcc $^-o $@ #$(bin2):$(src2)#gcc$^-o [email protected]:clean clean: rm -f $(bin1)#rm-f $(bin1) $(bin2)[gsm@VM-4-3-centos lesson7]$ make gcc proc.c -o proc [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>#include<unistd.h>intmain(){printf("hello Linux!");sleep(2);return0;}[gsm@VM-4-3-centos lesson7]$ make gcc proc.c -o proc [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ man fflush [gsm@VM-4-3-centos lesson7]$ man stdin[gsm@VM-4-3-centos lesson7]$ ll total 24-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 206 Oct 1221:01 Makefile -rwxrwxr-x 1 gsm gsm 8408 Oct 1221:03 proc -rw-rw----1 gsm gsm 112 Oct 1221:03 proc.c [gsm@VM-4-3-centos lesson7]$ vim proc.c [gsm@VM-4-3-centos lesson7]$ cat proc.c #include<stdio.h>#include<unistd.h>intmain(){printf("hello Linux!");fflush(stdout);sleep(2);return0;}[gsm@VM-4-3-centos lesson7]$ make gcc proc.c -o proc [gsm@VM-4-3-centos lesson7]$ ./proc hello Linux![gsm@VM-4-3-centos lesson7]$ make clean rm -f proc #rm-f proc [gsm@VM-4-3-centos lesson7]$ ll total 12-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c -rw-rw-r--1 gsm gsm 206 Oct 1221:01 Makefile -rw-rw----1 gsm gsm 131 Oct 1221:29 proc.c
[gsm@VM-4-3-centos lesson7]$ mkdir count [gsm@VM-4-3-centos lesson7]$ ll total 16-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c drwxrwxr-x 2 gsm gsm 4096 Oct 1221:32 count -rw-rw-r--1 gsm gsm 206 Oct 1221:01 Makefile -rw-rw----1 gsm gsm 131 Oct 1221:29 proc.c [gsm@VM-4-3-centos lesson7]$ cd count/[gsm@VM-4-3-centos count]$ ll total 0[gsm@VM-4-3-centos count]$ touch Count.c [gsm@VM-4-3-centos count]$ ll total 0-rw-rw-r--1 gsm gsm 0 Oct 1221:33 Count.c [gsm@VM-4-3-centos count]$ ls > Makefile [gsm@VM-4-3-centos count]$ ll total 4-rw-rw-r--1 gsm gsm 0 Oct 1221:33 Count.c -rw-rw-r--1 gsm gsm 17 Oct 1221:33 Makefile [gsm@VM-4-3-centos count]$ vim Makefile [gsm@VM-4-3-centos count]$ ll total 4-rw-rw-r--1 gsm gsm 0 Oct 1221:33 Count.c -rw-rw-r--1 gsm gsm 61 Oct 1221:36 Makefile [gsm@VM-4-3-centos count]$ cat Makefile Count:Count.c gcc -o $@ $^.PHONY:clean clean: rm -f Count [gsm@VM-4-3-centos count]$ vim Count.c [gsm@VM-4-3-centos count]$ cat Count.c #include<stdio.h>#include<unistd.h>intmain(){int count =9;while(count >=0){printf("%d\n", count); count--;sleep(1);}return0;}[gsm@VM-4-3-centos count]$ make gcc -o Count Count.c [gsm@VM-4-3-centos count]$ ll total 20-rwxrwxr-x 1 gsm gsm 8408 Oct 1221:41 Count -rw-rw-r--1 gsm gsm 199 Oct 1221:40 Count.c -rw-rw-r--1 gsm gsm 61 Oct 1221:36 Makefile [gsm@VM-4-3-centos count]$ ./Count 9876543210[gsm@VM-4-3-centos count]$ make clean rm -f Count [gsm@VM-4-3-centos count]$ ll total 8-rw-rw-r--1 gsm gsm 199 Oct 1221:40 Count.c -rw-rw-r--1 gsm gsm 61 Oct 1221:36 Makefile [gsm@VM-4-3-centos count]$ vim Count.c [gsm@VM-4-3-centos count]$ cat Count.c #include<stdio.h>#include<unistd.h>intmain(){int count =9;while(count >=0){printf("%d\r", count);// \r回车,但是没有换行,也就没有刷新fflush(stdout); count--;sleep(1);}printf("\r\n");return0;}[gsm@VM-4-3-centos count]$ make gcc -o Count Count.c [gsm@VM-4-3-centos count]$ ./Count 0[gsm@VM-4-3-centos count]$ make clean rm -f Count [gsm@VM-4-3-centos count]$ vim Count.c [gsm@VM-4-3-centos count]$ cat Count.c #include<stdio.h>#include<unistd.h>intmain(){int count =10;while(count >=0){printf("%-2d\r", count);// \r回车,但是没有换行,也就没有刷新fflush(stdout); count--;sleep(1);}printf("\r\n");return0;}[gsm@VM-4-3-centos count]$ make gcc -o Count Count.c [gsm@VM-4-3-centos count]$ ./Count 0[gsm@VM-4-3-centos count]$ make clean rm -f Count [gsm@VM-4-3-centos count]$ ll total 8-rw-rw-r--1 gsm gsm 300 Oct 1222:17 Count.c -rw-rw-r--1 gsm gsm 61 Oct 1221:36 Makefile
[gsm@VM-4-3-centos lesson7]$ ll total 16-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c drwxrwxr-x 2 gsm gsm 4096 Oct 1222:17 count -rw-rw-r--1 gsm gsm 206 Oct 1221:01 Makefile -rw-rw----1 gsm gsm 131 Oct 1221:29 proc.c [gsm@VM-4-3-centos lesson7]$ mkdir processbar [gsm@VM-4-3-centos lesson7]$ ll total 20-rw-rw-r--1 gsm gsm 223 Oct 1220:15 code.c drwxrwxr-x 2 gsm gsm 4096 Oct 1222:17 count -rw-rw-r--1 gsm gsm 206 Oct 1221:01 Makefile -rw-rw----1 gsm gsm 131 Oct 1221:29 proc.c drwxrwxr-x 2 gsm gsm 4096 Oct 1222:21 processbar [gsm@VM-4-3-centos lesson7]$ cd processbar/[gsm@VM-4-3-centos processbar]$ ll total 0[gsm@VM-4-3-centos processbar]$ pwd /home/gsm/112/lesson7/processbar [gsm@VM-4-3-centos processbar]$ touch process.c [gsm@VM-4-3-centos processbar]$ touch process.h [gsm@VM-4-3-centos processbar]$ touch main.c [gsm@VM-4-3-centos processbar]$ ll total 0-rw-rw-r--1 gsm gsm 0 Oct 1222:36 main.c -rw-rw-r--1 gsm gsm 0 Oct 1222:36 process.c -rw-rw-r--1 gsm gsm 0 Oct 1222:36 process.h [gsm@VM-4-3-centos processbar]$ cp ../count/Makefile .[gsm@VM-4-3-centos processbar]$ ll total 4-rw-rw-r--1 gsm gsm 0 Oct 1222:36 main.c -rw-rw-r--1 gsm gsm 61 Oct 1222:37 Makefile -rw-rw-r--1 gsm gsm 0 Oct 1222:36 process.c -rw-rw-r--1 gsm gsm 0 Oct 1222:36 process.h [gsm@VM-4-3-centos processbar]$ vim Makefile [gsm@VM-4-3-centos processbar]$ cat Makefile process:main.c process.c gcc -o $@ $^.PHONY:clean clean: rm -f process [gsm@VM-4-3-centos processbar]$ vim process.h [gsm@VM-4-3-centos processbar]$ vim main.c [gsm@VM-4-3-centos processbar]$ cat process.h #pragmaonce//versionvoidProcess();[gsm@VM-4-3-centos processbar]$ cat process.c #include"process.h"#include<stdio.h>voidProcess(){printf("hello Process\n");}[gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include"process.h"intmain(){Process();printf("hello process\n");return0;}[gsm@VM-4-3-centos processbar]$ make gcc -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ll total 28-rw-rw-r--1 gsm gsm 117 Oct 1222:49 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rwxrwxr-x 1 gsm gsm 8424 Oct 1222:52 process -rw-rw-r--1 gsm gsm 91 Oct 1222:47 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ ./process hello Process hello process [gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 16-rw-rw-r--1 gsm gsm 117 Oct 1222:49 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 91 Oct 1222:47 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ man 3 usleep [gsm@VM-4-3-centos processbar]$ vim main.c [gsm@VM-4-3-centos processbar]$ vim process.c [gsm@VM-4-3-centos processbar]$ cat process.h #pragmaonce//versionvoidProcess();[gsm@VM-4-3-centos processbar]$ cat process.c #include"process.h"#include<stdio.h>#include<string.h>#include<unistd.h>#defineNUM101#defineSTYLE'#'// version 1voidProcess(){constchar* lable ="|/-\\";int len =strlen(lable);char bar[NUM];memset(bar,'\0',sizeof(bar));int cnt =0;while(cnt <=100){printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);fflush(stdout); bar[cnt]= STYLE; cnt++;usleep(50000);}printf("\r\n");}[gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include"process.h"intmain(){Process();return0;}[gsm@VM-4-3-centos processbar]$ make gcc -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ./process [####################################################################################################][100%][|][gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 16-rw-rw-r--1 gsm gsm 86 Oct 1315:50 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 496 Oct 1316:41 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ vim process.c [gsm@VM-4-3-centos processbar]$ cat process.h #pragmaonce//versionvoidProcess();[gsm@VM-4-3-centos processbar]$ cat process.c #include"process.h"#include<stdio.h>#include<string.h>#include<unistd.h>#defineNUM101#defineSTYLE'='// version 1voidProcess(){constchar* lable ="|/-\\";int len =strlen(lable);char bar[NUM];memset(bar,'\0',sizeof(bar));int cnt =0;while(cnt <=100){printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);fflush(stdout); bar[cnt]= STYLE; cnt++;if(cnt +1== NUM){printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);break;} bar[cnt]='>';usleep(50000);}printf("\r\n");}[gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include"process.h"intmain(){Process();return0;}[gsm@VM-4-3-centos processbar]$ make gcc -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ./process [====================================================================================================][100%][|][gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 16-rw-rw-r--1 gsm gsm 86 Oct 1315:50 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 661 Oct 1317:33 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ cp process.c process-backup20251013.c [gsm@VM-4-3-centos processbar]$ ll total 20-rw-rw-r--1 gsm gsm 86 Oct 1315:50 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 661 Oct 1317:34 process-backup20251013.c -rw-rw-r--1 gsm gsm 661 Oct 1317:33 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ vim process.c [gsm@VM-4-3-centos processbar]$ cat process.h #pragmaonce//versionvoidProcess();[gsm@VM-4-3-centos processbar]$ cat process.c #include"process.h"#include<stdio.h>#include<string.h>#include<unistd.h>#defineNUM101#defineSTYLE1'>'#defineSTYLE2'='// version 1voidProcess(){constchar* lable ="|/-\\";int len =strlen(lable);char bar[NUM];memset(bar,'\0',sizeof(bar));int cnt =0;while(cnt <100){ bar[cnt]= STYLE1;printf("[%-100s][%d%%][%c]\r", bar, cnt +1, lable[(cnt +1)% len]);fflush(stdout);usleep(100000); bar[cnt]= STYLE2;printf("[%-100s][%d%%][%c]\r", bar, cnt +1, lable[(cnt +1)% len]);fflush(stdout); cnt++;// if (cnt + 1 == NUM)// {// printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);// break;// }//// bar[cnt] = '>';//usleep(50000);}printf("\r\n");}[gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include"process.h"intmain(){Process();return0;}[gsm@VM-4-3-centos processbar]$ make gcc -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ./process [====================================================================================================][100%][|][gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 20-rw-rw-r--1 gsm gsm 86 Oct 1315:50 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 661 Oct 1317:34 process-backup20251013.c -rw-rw-r--1 gsm gsm 860 Oct 1317:54 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ cp process.c process-backup20251013_02.c [gsm@VM-4-3-centos processbar]$ ll total 24-rw-rw-r--1 gsm gsm 86 Oct 1315:50 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 860 Oct 1317:56 process-backup20251013_02.c -rw-rw-r--1 gsm gsm 661 Oct 1317:34 process-backup20251013.c -rw-rw-r--1 gsm gsm 860 Oct 1317:54 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h
[gsm@VM-4-3-centos processbar]$ man srand [gsm@VM-4-3-centos processbar]$ man 3 time [gsm@VM-4-3-centos processbar]$ vim main.c [gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include<unistd.h>#include<time.h>#include<stdlib.h>#include"process.h"constint base =20;double total =2048.0;// 2048MBdouble once =0.1;voiddownload(){double current =0.0;while(current < total){// 模拟下载行为int r =rand()% base +1;double speed = r * once; current += speed;if(current >= total){ current = total;}usleep(10000);printf("test:%.1lf/%.1lf\r", current, total);fflush(stdout);}printf("\n");}intmain(){srand(time(NULL));download();return0;}[gsm@VM-4-3-centos processbar]$ make gcc -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ./process test:2048.0/2048.0[gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 24-rw-rw-r--1 gsm gsm 657 Oct 1319:24 main.c -rw-rw-r--1 gsm gsm 74 Oct 1222:39 Makefile -rw-rw-r--1 gsm gsm 860 Oct 1317:56 process-backup20251013_02.c -rw-rw-r--1 gsm gsm 661 Oct 1317:34 process-backup20251013.c -rw-rw-r--1 gsm gsm 495 Oct 1319:02 process.c -rw-rw-r--1 gsm gsm 41 Oct 1222:47 process.h [gsm@VM-4-3-centos processbar]$ vim main.c [gsm@VM-4-3-centos processbar]$ vim process.c [gsm@VM-4-3-centos processbar]$ vim process.h [gsm@VM-4-3-centos processbar]$ cat process.h #pragmaonce//version//void Process();//void Process(double total, double current);voidFlushProcess(double total,double current);[gsm@VM-4-3-centos processbar]$ cat process.c #include"process.h"#include<stdio.h>#include<string.h>#include<unistd.h>#defineNUM101#defineSTYLE'='#definePOINT'.'#defineSPACE' 'constint pnum =6;// version 2:真实的进度条,应该根据具体的比如下载的量,来动态刷新进度voidFlushProcess(double total,double current){// 1. 更新当前进度的百分比double rate =(current / total)*100;//printf("test:%.1lf%%\r", rate);//fflush(stdout);// 2. 更新进度条主体char bar[NUM];// 我们认为,1% 更新一个等号memset(bar,'\0',sizeof(bar));for(int i =0; i <(int)rate; i++){ bar[i]= STYLE;}// 3. 更新旋转光标或者是其他风格staticint num =0;char points[pnum +1];memset(points,'\0',sizeof(points));for(int i =0; i < pnum; i++){if(i <= num){ points[i]= POINT;}else{ points[i]= SPACE;}} num++; num %= pnum;// 4. test && printfprintf("[%-100s][%.1lf%%]%s\r", bar, rate, points);fflush(stdout);}// version 1//void Process()//{// const char* lable = "|/-\\";// int len = strlen(lable);// char bar[NUM];// memset(bar, '\0', sizeof(bar));// int cnt = 0;//// while (cnt <= 100)// {// printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);// fflush(stdout);// bar[cnt] = STYLE;// cnt++;// usleep(50000);// }//// printf("\r\n");//}[gsm@VM-4-3-centos processbar]$ cat main.c #include<stdio.h>#include<unistd.h>#include<time.h>#include<stdlib.h>#include"process.h"typedefvoid(*flush_t)(double total,double current);// 这是一个刷新的函数指针类型constint base =100;double total =2048.0;// 2048MBdouble once =0.1;// 进度条的调用方式voiddownload(flush_t f){double current =0.0;while(current < total){// 模拟下载行为int r =rand()% base +1;double speed = r * once; current += speed;if(current >= total){ current = total;}usleep(10000);// 更新出了本次新的下载量// 根据真实的应用场景,进行动态刷新//Process(total, 1.0);f(total, current);//printf("test:%.1lf/%.1lf\r", current, total);//fflush(stdout);}printf("\n");}intmain(){srand(time(NULL));download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);return0;}[gsm@VM-4-3-centos processbar]$ make gcc -std=gnu99 -o process main.c process.c [gsm@VM-4-3-centos processbar]$ ./process [====================================================================================================][100.0%].....[====================================================================================================][100.0%]......[====================================================================================================][100.0%]....[====================================================================================================][100.0%].....[====================================================================================================][100.0%].[====================================================================================================][100.0%]..[====================================================================================================][100.0%].[gsm@VM-4-3-centos processbar]$ make clean rm -f process [gsm@VM-4-3-centos processbar]$ ll total 24-rw-rw-r--1 gsm gsm 1149 Oct 1412:15 main.c -rw-rw-r--1 gsm gsm 85 Oct 1400:52 Makefile -rw-rw-r--1 gsm gsm 860 Oct 1317:56 process-backup20251013_02.c -rw-rw-r--1 gsm gsm 661 Oct 1317:34 process-backup20251013.c -rw-rw-r--1 gsm gsm 1556 Oct 1412:15 process.c -rw-rw-r--1 gsm gsm 138 Oct 1412:15 process.h