网络 / 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
相关推荐
踩着阴暗的自己向上爬2 小时前
Day05-04-持续集成总结
linux·运维·ci/cd
qyhua4 小时前
Linux内网端口转公网端口映射
linux·运维·服务器
j.king5 小时前
开源GTKSystem.Windows.Forms框架让C# winform支持跨平台运行
linux·c#·gtk
安步当歌5 小时前
【FFmpeg】av_write_trailer函数
c语言·c++·ffmpeg·视频编解码·video-codec
stackY、6 小时前
【Linux】:程序地址空间
linux·算法
R语言爱好者7 小时前
如何查看程序是否在运行-Linux
linux
逆风就重开7 小时前
数据埋点从入门到了解
大数据·linux·前端·数据库·数据仓库
HanLop7 小时前
C语言-动态内存管理
c语言·开发语言
掘根7 小时前
【Linux】压缩命令——gzip,bzip2,xz
大数据·linux·运维
EthanWsir7 小时前
嵌入式C语言面试相关知识——关键字(不定期更新)
c语言·开发语言·面试