Linux systems commonly used commands (3)

1. man command

2. echo command

Example 1:

Example 2: Check the value of a variable in the environment

3. vim three modes

vim is not with the mouse, we need to operate the file with the command.

4. vim cursor position is deleted _ undo

Note: the cursor in front of the actual position of the character covered.

5. vim of copy and paste

Copy the current cursor line is the command yy, delete is actually cut.

6. vim visual mode and find

      

Let the current line indented to the right: >>    

Let the current line indented to the right: <<

Check the man page: When the operation to find the cursor at this time to look at the contents of the body, then shift + k can enter the position it for the first time in man the document. If at this time 3 + shift + k can specify the jump to man the third chapter of the document.

7. command mode to text mode

Note:. 1 a i and the front and behind the insertion position of the cursor, the cursor position herein refers to the character cursor is covered, is inserted in the back and front of the character. 2. s delete the cursor behind the character refers to covered cursor character.

8. The line mode-related operations

(1) Find and Replace:

         1): a first tom s / tom / jack into the cursor line jack.

         2): All tom s / tom / jack / g to the cursor line are replaced jack.

         3):% s / tom / jack tom each document a first row is replaced by jack.

         4):% s / tom / jack / g in all documents have been replaced by tom jack.

         5): tom 27,30s / tom / jack / g only the first row is replaced by 27-30 jack.

(2) in a line mode with:! + Command to execute the command::! Pwd or:! Ls will output information such as:

         

(3) save and exit

        

9. vi split screen

(1): sp vertically split screen display

(2): vsp left split screen display

(3):sp + 文件名字   /   :vsp + 文件名字         新打开的文件和之前的文件就会一起显示。   如果修改内容了的话,需要在保存之后两个才会显示的一样。

(4)ctrl + w + w    在两个分屏之间进行切换。

(5):q  关掉一个分屏     :qw   关掉并保存一个分屏    :qwall   关掉并保存所有分屏,等等组合。

10. vim的配置文件

11. gcc的编译过程

例:

12. gcc的一些参数

(1)gcc sum.c -o myapp      这样就可以直接生成myapp这个可执行文件。

(2)gcc sum.c -I ./include -o app     如果头文件不在当前目录下,需要指定头文件所在的目录(比如在./include),用 -I 参数指定。

(3)下面这段代码define了一个宏DEBUG,并且在要printf时判断是否定义这个宏了,如果定义了就执行printf。

那么如果把#define DEBUG给注释掉,那么再生成并执行的程序就不输出打印的内容了。我们用一个方法也可以加宏,就是在编译程序的时候,用参数-D DEBUG指定宏就可以了,这样就又可以输出打印内容了,如下:

(4)-O1、-O2、-O3   这三个参数可以不同程度优化程序,可以把冗余的代码做一个优化,其中O3优化的级别最高。

(5)-Wall 参数可以在程序编译时输出警告信息。

(6)-g 参数是在程序中添加一些调试信息。(gdb调试的时候必须加此参数)

13. 静态库的制作和使用

例:

Calc目录中有一个main.c文件,还有3个目录:include、lib、src。

打开src目录中的一个文件,比如add.c:

里面是add的实现:

打开include目录中的head.h头文件,里面是函数的声明:

下面开始生成静态库,先是第一步,生成对应的.o文件:

然后是第二步,将生成的.o文件打包:

这样就生成了静态库libMyCalc.a,然后把它移动到lib目录中去:

现在把include目录和lib目录给用户,他们就可以用了。下面是在main.c中使用:

生成可执行文件sum并执行(方法1):

生成可执行文件sum并执行(方法2): -L参数用来指定静态库所在目录, -l参数用来指定静态库的名称(去掉头尾)。

14. 静态库的优缺点

如果程序中只调用了函数a()和函数b(),那么编译生成的可执行文件中只包含add.o和sub.o。

静态库的优点:

(1)发布程序的时候,不需要提供对应的库

(2)加载库的速度快

静态库的缺点:

(1)库被打包到应用程序中,导致可执行文件体积较大。

(2)库发生了改变,需要重新编译程序。

15. 共享库的制作

例:

生成与位置无关的代码:

用静态库生成可执行文件后,.o文件在程序运行初始时就在代码段中了,位置是固定的,称为与位置有关的.o;

而用共享库时,在程序运行起来之后共享库才被逐步被加载到  共享库区,每次位置不一定相同,称为与位置无关的.o。

上面已经生成.o了,现在生成动态库.so:

然后把.so移动到 lib目录中:

现在把include目录和 lib目录给用户,他们就可以用了。

16. 共享库的使用

生成可执行文件(方法1):

生成可执行文件(方法2):

现在运行可执行文件会发现运行不了,这是因为找不到动态库libMyCalc.so。

可执行程序在执行时,动态链接器会自动调用需要使用的动态库。

ldd命令可以查看可执行文件依赖的动态库,下图红框框起来的动态库就是动态链接器。

可以看到自己创建的动态库 libMyCalc.so 在程序运行时是不能被找到的,我们有以下方法来解决:

推荐使用第4种方法。解决后可以看到程序可以正常运行了。

17. 共享库的优缺点

共享库的优点:

(1)可执行文件体积小。

(2)动态库更新了,不需要重新编译程序(函数接口不变时)。

共享库的缺点:

(1)发布程序的时候,需要将动态库提供给客户。

(2)动态库没有被打包到应用程序中,加载速度相对较慢。

 

Guess you like

Origin blog.csdn.net/mengyujia1234/article/details/90485434
Recommended