Linux操作系统安装

安装教程:硬盘安装Ubuntu,重装Linux系统

如果开机的时候,没有出现选择哪个系统的界面,参照以下文章解决(2.6)
https://luozm.github.io/win-ubuntu
https://www.xiaoyuanjiu.com/10832.html

如果出现Ubuntu引导错误,”Minimal BASH-like line editingis supported…”,参照以下文章解决
http://www.voidcn.com/article/p-vbifqnft-beo.html
https://linux.cn/article-5909-1.html

解决安装Linux系统后导致window系统时间异常的问题

  1. #安装时间校准服务
  2. $ sudo apt-get install ntpdate
  3. #从time.windows.com获取本地时间
  4. $ sudo ntpdate time.windows.com
  5. #同步时间到硬件
  6. $ sudo hwclock --localtime --systohc

vim编辑器

  1. sudo apt-get update // 更新源
  2. sudo apt-get install vim // 安装vim编辑器

apt:Advanced Packaging Tool

命令模式:

i:进入编辑模式
:sp max.c:新开一个max.c窗口
:set nu:显示行号
:[num]:跳转到某一行
[num]:从当前行向下跨越多少行
ctrl + w + 上箭头:跳转到上面窗口
ctrl + w + 下箭头:跳转到下面窗口

替换
:{作用范围}s/{目标}/{替换}/{替换标志}
例如:%s/foo/bar/g会在全局范围(%)查找foo并替换为bar,所有出现都会被替换(g

image.png

我的Vim配置:~/.vimrc

  1. syntax on
  2. set showcmd
  3. set autoindent
  4. set expandtab
  5. set softtabstop=4

注意:配置Makefile文件的时候,需要默认的Tab,这时先Ctrl+v,然后按Tab,就还用使用默认Tab。
Vim 配置入门

gcc编译器

  1. // 安装
  2. sudo apt install gcc
  3. // 使用
  4. gcc test.c // 编译后输出a.out文件(默认)
  5. gcc test.c -o test.out // 编译后输出test.out文件
  6. gcc -c max.c -o max.o // 单独编译max.c文件,输出文件为max.o

gcc:GNU C Compiler C编译器 g++:GNU C++ Compiler C++编译器

make工具

  • make工具可以将大型的开发项目分成若干个模块
  • make工具可以很清晰和快捷的整理源文件
  1. // 安装
  2. sudo apt install make
  1. // main.c
  2. #include <stdio.h>
  3. #include "max.h"
  4. #include "min.h"
  5. int main()
  6. {
  7. int a1=33;
  8. int a2=21;
  9. int maxNum=max(a1,a2);
  10. int minNum=min(a1,a2);
  11. printf("the max value is %d\n",maxNum);
  12. printf("the min value is %d\n",minNum);
  13. return 0;
  14. }
  15. // max.h
  16. int max(int a, int b);
  17. // max.c
  18. int max(int a,int b)
  19. {
  20. return a>b?a:b;
  21. }
  22. // min.h
  23. int min(int a, int b);
  24. // min.c
  25. int min(int a,int b)
  26. {
  27. return a>b?b:a;
  28. }
  1. // Makefile
  2. #this is make file
  3. main.out:max.o min.o main.c
  4. gcc max.o min.o main.c -o main.out
  5. max.o:max.c
  6. gcc -c max.c
  7. min.o:min.c
  8. gcc -c min.c

main函数的return

  1. // main.c
  2. #include <stdio.h>
  3. int main(int argv,char* argc[])
  4. {
  5. printf("hello world!\n");
  6. return 0;
  7. }

思考一下:main函数中,为什么要return 0?reutrn 其他不行吗?

  1. gcc main.c -o main.out // 生成main.out文件
  2. ./main.out // 执行mian.out文件处理
  3. hello world // 显示结果:hello world
  4. echo $? // 查看上一次的执行结果是否正常执行,如果正常返回0,反之返回错误代码
  5. 0 // 显示0,说明执行正常
  6. ./main.out && ls // 先执行./mian.out,如果正常再执行ls
  7. hello world! // ./main.out的执行结果,正常
  8. main.c main.out // ls的执行结果

从以上结果中可以看到,mian.out执行后的结果代码是0,说明正常执行了。那这个0是哪里来的呢?从main函数中可以看到,就是 return 0; 这个处理,那我们试着把返回值设置成其他值。

  1. // main.c
  2. #include <stdio.h>
  3. int main(int argv,char* argc[])
  4. {
  5. printf("hello world!\n");
  6. return 104;
  7. }
  1. gcc main.c -o main.out // 生成main.out文件
  2. ./main.out // 执行mian.out文件处理
  3. hello world // 显示结果:hello world
  4. echo $? // 查看上一次的执行结果是否正常执行,如果正常返回0,反之返回错误代码
  5. 104 // 显示104,说明执行异常
  6. ./main.out && ls // 先执行./mian.out,如果正常再执行ls
  7. hello world! // ./main.out的执行结果

可以看到,./main.out执行之后,ls并没有执行,这是因为./main.out的执行结果是异常的。
所以,main函数的 return 0;不是随便写的,是有意义的,代表了main函数正常执行。当然,我们可以根据业务,在出现异常的时候,返回相应的异常代码。

main函数中的参数

  1. // main.c
  2. #include <stdio.h>
  3. int main(int argv,char* argc[])
  4. {
  5. printf("argv is %d\n",argv);
  6. int i;
  7. for(i=0;i<argv;i++){
  8. printf("argc[%d] is %s \n",i,argc[i]);
  9. }
  10. return 0;
  11. }
  1. gcc main.c -o main.out // 生成main.out文件
  2. ./main.out // 执行main.out处理
  3. argv is 1 // 执行结果1
  4. argc[0] is ./main.out // 执行结果2
  5. ./main.out -l -a -p // 执行main.out处理,带参数
  6. argv is 4 // 执行结果1
  7. argc[0] is ./main.out // 执行结果2
  8. argc[1] is -l // 执行结果3
  9. argc[2] is -a // 执行结果4
  10. argc[3] is -p // 执行结果5

以上应该能很清楚的看到,main函数中两个参数的含义。第一个参数表示有几个命令,第二个命令表示这几个命令分别是什么。./main.out和linux的ls命令一样,都是可以带参数的。为什么c语言与linux如此相似?连他们命令参数的设置方法都是一样的?这是因为linux本身就是用C语言编写的。linux源自unix,unix和C语言是同一个人(丹尼斯·里奇)发明的。大神,膜拜。

输入/输出/错误流的重定向

  1. #include <stdio.h>
  2. int main()
  3. {
  4. printf("input the int value i:\n");
  5. int i,j;
  6. scanf("%d",&i);
  7. printf("input the int value j:\n");
  8. scanf("%d",&j);
  9. printf("i+j=%d\n",i+j);
  10. return 0;
  11. }

:输出重定向,在文件尾部追加

  1. chenys@chenys-VirtualBox:~/workspace/les4$ ./main.out >> log.txt
  2. 33
  3. 44
  4. chenys@chenys-VirtualBox:~/workspace/les4$ ./main.out >> log.txt
  5. 55
  6. 66
  7. chenys@chenys-VirtualBox:~/workspace/les4$ ls
  8. log.txt main.c main.out
  9. chenys@chenys-VirtualBox:~/workspace/les4$ cat log.txt
  10. input the int value i:
  11. input the int value j:
  12. i+j=77
  13. input the int value i:
  14. input the int value j:
  15. i+j=121

:输出重定向,覆盖文件内容

  1. chenys@chenys-VirtualBox:~/workspace/les4$ ./main.out > log.txt
  2. 100
  3. 200
  4. chenys@chenys-VirtualBox:~/workspace/les4$ cat log.txt
  5. input the int value i:
  6. input the int value j:
  7. i+j=300

<:输入重定向,没有<<因为输入必定只有从一个文件中来

  1. chenys@chenys-VirtualBox:~/workspace/les4$ cat input.txt
  2. 23
  3. 54
  4. chenys@chenys-VirtualBox:~/workspace/les4$ ./main.out < input.txt
  5. input the int value i:
  6. input the int value j:
  7. i+j=77

1>XX:输出流的重定向(正常)
2>XX:错误流的重定向

  1. chenys@chenys-VirtualBox:~/workspace/les4$ ./main.out 1>ok.txt 2>ng.txt
  2. 7
  3. 0
  4. chenys@chenys-VirtualBox:~/workspace/les4$ ls
  5. input.txt main.c main.out ng.txt ok.txt
  6. chenys@chenys-VirtualBox:~/workspace/les4$ cat ok.txt
  7. input the int value i:
  8. input the int value j:
  9. chenys@chenys-VirtualBox:~/workspace/les4$ cat ng.txt
  10. j!=0