网络 / day06 作业

1. sqlite3 操作

1> 创建一个工人信息库,包含工号(主键)、姓名、年龄、薪资。

sql 复制代码
sqlite> create table worker
   ...> (
   ...> id integer primary key autoincrement,
   ...> age int(2),
   ...> name varchar(20),
   ...> salary double
   ...> );
sqlite> .table
worker
sqlite> .schema
CREATE TABLE worker
(
id integer primary key autoincrement,
age int(2),
name varchar(20),
salary double
);
CREATE TABLE sqlite_sequence(name,seq);

2> 添加三条工人信息(可以完整信息,也可以非完整信息)

sql 复制代码
sqlite> insert into worker (age, name, salary) values(20, "zhangsan", 20000);
sqlite> insert into worker (age, name, salary) values(21, "lisi", 21000);
sqlite> insert into worker (age, name, salary) values(22, "wangwu", 22000);
sqlite> select * from worker
   ...> ;
1|20|zhangsan|20000.0
2|21|lisi|21000.0
3|22|wangwu|22000.0
sqlite> .header on
sqlite> .mode column
sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      22000.0 

3> 修改某一个工人的薪资(确定的一个)

sql 复制代码
sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      22000.0   
sqlite> update worker set salary=23000 where id=3;
sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      23000.0 

4> 展示出工资在10000到20000之间的所有工人信息

cpp 复制代码
sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
2           21          lisi        21000.0   
3           22          wangwu      13000.0   
sqlite> select * from worker where salary between 10000 and 20000
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
3           22          wangwu      13000.0  

5> 删除掉指定姓名工人的信息

cpp 复制代码
sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
2           21          lisi        21000.0   
3           22          wangwu      13000.0   
sqlite> delete from worker where name="lisi";
sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
3           22          wangwu      13000.0 

6> 工厂倒闭,删除整个工人信息库

sql 复制代码
sqlite> drop table worker
   ...> ;
sqlite> .table

2. 静态库制作

2.1. 原始文件

主程序 main.c

cpp 复制代码
#include "test.h"

int main(int argc, const char *argv[])
{
	int res = sum(1, 2);
	printf("res=%d\n", res );
	
	return 0;
}

目标库文件 test.c

cpp 复制代码
int sum(int a, int b)
{
	return a + b;
}

头文件 test.h

cpp 复制代码
#ifndef __TEST_H__
#define __TEST_H__

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int sum(int a, int b);

#endif 

目录结构

cpp 复制代码
.
├── head
│   └── test.h
├── lib
└── src
    ├── main.c
    └── test.c

3 directories, 3 files

2.2 通过test.c制作成静态库

2.2.1. 将源文件只编译不链接生产二进制文件

bash 复制代码
gcc -c test.c -o ../lib/test.o


.
├── head
│   └── test.h
├── lib
│   └── test.o
└── src
    ├── main.c
    └── test.c

2.2.2. 将上述生成的二进制文件制作成静态库

ar -crs lib库名.a test.o

ar:用于生产静态库的指令

-c:表时创建静态库

-r:将文件插入或替代之前库中的名称

-s:重置静态库

bash 复制代码
ar -crs ../lib/libtest.a ../lib/test.o


.
├── head
│   └── test.h
├── lib
│   ├── libtest.a
│   └── test.o
└── src
    ├── main.c
    └── test.c

2.2.3. 使用静态库

gcc 主程序.c -L 库的路径 -l库名 -I 头文件路径

bash 复制代码
ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc main.c -L ../lib/ -ltest -I ../head/ -o test
ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ ls -la
total 28
drwxrwxr-x 2 ubuntu ubuntu 4096 12月 21 19:17 .
drwxrwxr-x 5 ubuntu ubuntu 4096 12月 21 15:49 ..
-rw-rw-r-- 1 ubuntu ubuntu   87 12月 21 15:27 main.c
-rwxrwxr-x 1 ubuntu ubuntu 8224 12月 21 19:17 test
-rw-rw-r-- 1 ubuntu ubuntu   42 12月 21 15:28 test.c

3. 动态库制作

3.1. 原始文件同2

bash 复制代码
.
├── head
│   └── test.h
├── lib
└── src
    ├── main.c
    └── test.c

3.2. 生成动态库

gcc -fPIC -c test.c -o test.o //-fPIC忽略文件位置将 test.c生产test.o

gcc -shared test.o -o libtest.so //将二进制文件编译生产库文件

也可以统一成如下指令

gcc -fPIC -shared test.c -o libtest.so

bash 复制代码
ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc -fPIC -shared test.c -o ../lib/libtest.so



.
├── head
│   └── test.h
├── lib
│   └── libtest.so
└── src
    ├── main.c
    └── test.c

3.3 使用动态库

gcc 主程序.c -L 库的路径 -l库名 -I 头文件路径

cpp 复制代码
ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc main.c -L ../lib/ -ltest -I ../head/ -o test

ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ ls -la
total 28
drwxrwxr-x 2 ubuntu ubuntu 4096 12月 21 19:30 .
drwxrwxr-x 5 ubuntu ubuntu 4096 12月 21 15:49 ..
-rw-rw-r-- 1 ubuntu ubuntu  124 12月 21 19:19 main.c
-rwxrwxr-x 1 ubuntu ubuntu 8328 12月 21 19:30 test
-rw-rw-r-- 1 ubuntu ubuntu   42 12月 21 15:28 test.c
相关推荐
海棠蚀omo1 小时前
Linux操作系统-父进程的等待:一个关于回收与终结的故事
linux·操作系统
乌萨奇也要立志学C++1 小时前
【Linux】Ext系列文件系统 从磁盘结构到文件存储的原理剖析
android·linux·缓存·1024程序员节
软安科技1 小时前
专有软件使用Linux内核的用户头文件违反GPL吗?| 开源合规场景
linux·开源软件·开源协议
A-刘晨阳1 小时前
K8S 二进制集群搭建(一主两从)
linux·运维·云原生·容器·kubernetes
egoist20231 小时前
[linux仓库]信号处理[进程信号·伍]
linux·信号处理·写时拷贝·软中断·硬件中断·缺页中断·时钟中断
一碗绿豆汤2 小时前
C语言-结构体
c语言·开发语言
HIT_Weston2 小时前
15、【Ubuntu】【VSCode】VSCode 断联问题分析:UID 补充
linux·vscode·ubuntu
碰大点2 小时前
Ubuntu 16.04交叉编译arm-linux-gnueabihf的QT5.6.2
linux·arm开发·qt·ubuntu·arm-linux
小-黯2 小时前
Linux硬盘挂载脚本
linux·运维·服务器
PeaceKeeper72 小时前
简易的arm-linux库文件移植
linux·运维·arm开发