The use of Vim, gcc compilation, the generation and use of static libraries and shared libraries

The use of vim, gcc compilation, the generation and use of static libraries and shared libraries

# Learning objectives:

  1. To master the use of related commands in vim command mode
  2. To master the related commands to switch from command mode to editing mode
  3. To master the use of related commands in vim last-line mode
  4. Be able to tell the workflow of gcc Master the use of common parameters
  5. Proficiency in the production and use of static libraries
  under Linux 6. Proficiency in the production and use of shared libraries under Linux


Learning content

1. Use of Vim
2. Use of gcc compiler
3. Use of static library
under Linux 4. Use of dynamic library under Linux


Study time
 January 22, 2021


1. Vim editor

  1. vim briefly introduces

    vi—"virtual interface". Its status on Linux is like a notepad in Windows. It can perform edit, delete, search, replace, block operations, and many other text operations, and users can adjust according to their own needs. For its customization, vi is a text editing program, it is not like a beautiful visual interface in Windows Notepad; use commands to operate it.

  1. There are three modes in vim:
     command mode, edit mode, last line mode, the relationship between the three is as follows
    Insert picture description here


1.1. Command mode

command operating
ZZ Save and exit to the shell command line
gg=G Code formatting
h Cursor left
j Cursor down
k Cursor right
w Move one word distance
gg Cursor to the beginning of the file
G Move the cursor to the end of the file
0 Cursor to the first line
$ Cursor to the end of the line
nG Line jump, example 12G, jump to line 12
x Delete one character after the cursor, equivalent to Del
X Delete the character before the cursor, equivalent to Backspace
dw Delete the word at the beginning of the cursor, including the character at the cursor
d0 Delete all the contents of the line before the cursor, excluding the character where the cursor is
D All the contents of this line after deleting the cursor, including the character where the cursor is located
dd Delete the line where the cursor is (its essence is to cut)
ndd Delete the specified function from the current line of the cursor, such as 15dd
ctrl+v But in mode, you can move the cursor to select the specified content, and then make further instructions
u Revocation step by step, equivalent to ctrl+z in word
yy Copy current line
nyy Copy n rows, such as 10yy
p Open a new line down at the cursor position and paste
P Open up a new line at the cursor position and paste
Cut operation Press dd or ndd to delete, save the deleted lines to the clipboard, and then press p/P to paste
r Replace current character
R Replace the character after the cursor in the current line
/ /xxxx, search from the cursor position, press n to search down, press N to search up
? ?xxxx, search from the cursor position, press n to search upwards, press N to search downwards
# Move the cursor to the character with search, then press n to search up, press N to search down
shift+k Press shift+k or k on the character with search, you can view related help documents

1.2 Edit mode

Enter the following commands from command mode and edit mode order:

command operating
i Insert before the cursor
a Insert after the cursor
I Insert at the beginning of the line where the cursor is
A Insert at the end of the line where the cursor is
O Create a new line below the line where the cursor is, insert at the beginning of the line
O Create a new line above the line where the cursor is, insert at the beginning of the line
s Delete the character behind the cursor, insert from the current position of the cursor
S Delete the current line where the cursor is located, insert at the beginning of the line

1.3 Last line mode

Switch from command mode to last line mode: enter a colon (:)


command operating
q drop out
q! Force quit
w Save changes
wq Save and exit
x Save and exit
:s/old/new/ Replace the first old in the line where the cursor is with new
:s/old/newg Replace all old where the cursor is with new
:m,ns/old/new/g Replace all old between the mth line and the nth line with new
:%s/old/g Replace all old in the current file with new
:1,$s/old/new/g Replace all old bodies of the current file with new (the first line to the last line)
:%s/old/new/gc Same as above, but each replacement requires user confirmation
ctrl+u Scroll down half the screen (move the cursor up)
ctrl+d Flip up half the screen (the cursor moves down)
ctrl+f Flip up one screen
ctrl+b Flip back one screen
sp Current file horizontal split screen
vsp Current file vertical split screen
sp 文件名 当前文件和另一文件水平分屏
vsp 文件名 当前文件和另一文件垂直分屏
ctrl+w+w 多个窗口切换光标
wall/wqall/xall/qall/qall! 保存/保存退出/保存退出/退出/强制退出 分屏窗口

1.4vim的配置文件

用户级别的文件配置
 ~./vimrc,修改用户级别的配置文件文件指挥影响当前用户,不会影响其他用户
 例如在:在用户的家目录下的.vimrc 文件 添加
set nu 设置行号
set tabstop=4 设置缩进为4
set shiftwidth=4 设置gg=G缩进4个空格,默认缩进8个空格


系统级别的文件配置,不推荐!!

2.gcc 编译器

  • 参数
  • -v #参看gcc版本号,----version也行
  • -E #生成预处理文件
  • -S #生成汇编文件
  • -c #只编译,生成.o文件,可重定位文件
  • -I #指定同文件所在的路径
  • -L #指定库文件所在的路径
  • -l #指定库的名字
  • -o #指定生成的目标文件的名字
  • -g #包含调试信息,使用gdb调试需要添加参数-g
  • -On n=0~3 #编译优化级别
  • -D #编译时定义宏

3.静态库和共享库

3.1.1 什么是库

  • 库是二进制文件, 是源代码文件的另一种表现形式, 是加了密的源代码; 是一些功能相近或者是相似的函数的集合体.

3.1.2 库的作用

  • 提高代码的可重用性, 而且还可以提高程序的健壮性;
  • 可以减少开发者的代码开发量, 缩短开发周期.

3.1.3 使用方法

  • 头文件---包含了库函数的声明
  • 库文件---包含了库函数的代码实现
    注意: 库不能单独使用, 只能作为其他执行程序的一部分完成某些功能, 也 就是说只能被其他程序调用才能使用.

3.2.1 静态库

  • 静态库可以认为是一些目标代码的集合, 是在可执行程序运行前就已经加入到执行码中, 成为执行程序的一部分. 按照习惯, 一般以.a做为文件后缀名, 静态库的命名一般分为三个部分:
  • 前缀:lib
  • 库名称:自定义即可, 如test
  • 后缀:.a
    最终的静态库的文件名为:libtest.a

  1. 静态库的制作:
  2. 将c源文件生成对应的.o文件
    gcc -c fun1.c fun2.c
    或者分别生成.o文件:
    gcc -c fun1.c -o fun1.o
    gcc -c fun2.c -o fun2.o

  3. 使用打包工具ar将准备好的.o文件打包为.a文件
    在使用ar工具是时候需要添加参数rcs r更新、c创建、s建立索引
    命令:ar rcs 静态库名 .o文件 ar rcsbtes lit1.a fun1.o fun2.o

  • 静态库的使用
      静态库制作完成之后, 需要将.a文件和头文件一定发布给用户. 假设测试文件为main.c, 静态库文件为libtest1.a, 头文件为head.h 用到的参数:
  • -L:指定要链接的库的所在目录

  • -l:指定连接时需要的静态库,去掉前缀和后缀
  • -I:指定main.c文件用到的头文件head.h所在的路径

3.2.2 共享库

  • The shared library is not linked to the target code when the program is compiled, but is loaded when the program is running. If different applications call the same library, then only one copy of the shared library is required in the memory , Avoid the problem of space waste. The dynamic library is loaded when the program is running, and it also solves the problem of updating, deploying and publishing the program by the static library. The user only needs to update the dynamic library, incremental update. Why The need for dynamic libraries is actually caused by the characteristics of static libraries. According to custom, ".so" is generally used as the file extension. The naming of shared libraries is generally divided into three parts:
  • Prefix: lib
  • Library name: Customize, such as test
  • Suffix: .so
    The file name of the final dynamic library: libtest.a

  1. The production of dynamic library:
  2. Generate the object file .o, at this time you need to add the compiler option: -fPIC (fpic)
    gcc -fpic -c fun1.c fun2.c Parameters: -fpic
    creates an address-independent compiler, the purpose is to be able to be used in multiple applications Shared between.

  3. To generate a shared library, add linker options at this time: -shared (specify to generate a dynamic link library)
    gcc -shared fun1.o fun2.o -o libtest2.so

  • Use of dynamic library :
      reference dynamic library and compile into executable file (same as static library method): Parameters used:
  • -L: Specify the directory of the library to be linked

  • -l: Specify the static library required for connection, remove the prefix and suffix

  • -I: Specify the path where the header file head.h used by the main.c file is located
    gcc main.c -l./ -L./ -ltest2 -o main2

3.3 Advantages and disadvantages of static libraries and shared libraries

3.1 Static library

advantage:

	1.执行速度快, 是因为静态库已经编译到可执行文件内部了
	2.移植方便, 不依赖域其他的库文件

Disadvantages:

	1.耗费内存, 是由于每一个静态库的可执行程序都会加载一次
	2.部署更新麻烦, 因为静态库修改以后所有的调用到这个静态库的可执行文件都需要重新编译

3.2 Dynamic library

advantage:

	1.节省内存
	2.部署升级更新方便, 只需替换动态库即可, 然后再重启服务.

Disadvantages:

	1.加载速度比静态库慢
	2.移植性差, 需要把所有用到的动态库都移植.

Guess you like

Origin blog.csdn.net/Woo_Hoo__/article/details/113001785