Linux C++ 网络编程学习系列(7)——mbedtls编译使用
mbedtls编译使用
- 环境: Ubuntu18.04
- 编译器:gcc或clang(windows下可用vs编译,或者mingw编译,官网有详细教程,参见:https://tls.mbed.org/kb/compiling-and-building/compiling-mbedtls-in-mingw)
- 编译选项: 静态编译使用
1. mbedtls源码
下载地址: https://github.com/ARMmbed/mbedtls/releases
也可以用git clone, 然后切换到相应版本.
ing@ubuntu:~/opt$ wget https://github.com/ARMmbed/mbedtls/archive/mbedtls-2.16.5.tar.gz
ing@ubuntu:~/opt$ tar -xf mbedtls-mbedtls-2.16.5.tar.gz
ing@ubuntu:~/opt$ cd mbedtls-mbedtls-2.16.5/
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5$ mkdir build
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5$ cd build/
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5/build$ cmake ..
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5/build$ make
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5/build$ ls include/mbedtls
aes.h ...
ing@ubuntu:~/opt/mbedtls-mbedtls-2.16.5/build$ ls library/ -la
-rw-rw-r-- 1 ing ing 744638 Mar 28 19:32 libmbedcrypto.a
-rw-rw-r-- 1 ing ing 351816 Mar 28 19:32 libmbedtls.a
-rw-rw-r-- 1 ing ing 173904 Mar 28 19:32 libmbedx509.a
注意:
- 这样编译完成之后, 生成include目录下的头文件, 以及library下的.a静态库文件(默认就是静态编译), 也可以生成动态共享库.so
- 使用时就用.h和.a文件就好了
- 编译完成之后也可以
sudo make install, 这样就会把include和library中的.h文件和.a文件分别复制到默认路径/usr/local/include/和/usr/local/lib/目录下. 这样的话我们就不用每次复制.h和.a文件了, 不过我自己不用这个, 我喜欢哪个项目需要就复制过去, 见下例.
2. mbedtls库使用
完整项目示例参见(这个也是cmakelists.txt编写的示例): https://github.com/whuwzp/vim_config/blob/master/test/cmake_example
cmakelists.txt的多文档组织编写说明: https://www.cnblogs.com/whuwzp/p/cmakefiles.html, https://whuwzp.github.io/LinuxCpp-NetWork-0-makefile学习.md
项目目录结构:(省略了build目录)
$ ~/test/cmake_example$ tree
.
├── bin # 生成可执行文件的文件夹
│ └── main
├── CMakeLists.txt # 根目录cmakelists.txt
├── hello1 # 子目录1
│ ├── CMakeLists.txt # 子目录1的cmakelists.txt
│ ├── hello1.cpp
│ └── hello1.h
├── hello2 # 子目录2
│ ├── CMakeLists.txt # 子目录2的cmakelists.txt
│ ├── hello2.cpp
│ └── hello2.h
├── main.cpp # main函数所在源文件, 需要调用hellofunc1和hellofunc2
└── mbedtls # 第三方库
├── include # 第三方库的头文件
│ └── mbedtls
│ ├── aes.h # 省略了mbedtls的其他头文件
│ └── xtea.h
└── lib # 第三方库的库文件
├── libmbedcrypto.a
├── libmbedtls.a
└── libmbedx509.a
其中main.cpp就是复制mbedtls-mbedtls-2.16.5/programs/ssl/ssl_server.c的.
3. -fPIC
有需求如下:
- 使用静态编译后的mbedtls给我的项目test使用
- test最终想要编译为动态链接库,即shared
如果按照上述方法使用.a文件, 编译test时报错如下:
libmbedtls.a(ssl_tls.c.o): relocation R_X86_64_PC32 against symbol mbedtls_x509_crt_profile_suiteb can not be used when making a shared object; recompile with -fPIC
意思是让重新带上-fPIC编译(自己百度-fPIC的用途)
于是:
# add -fPIC
[mbedtls-mbedtls-2.16.5]$ vim CMakeLists.txt
# 在CMAKE_C_FLAGS标识中增加-fPIC
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclarati on-after-statement -Wwrite-strings -fPIC")
cmake ..
make
# 然后再用.a文件即可
3. 参考网址
- mbedtls下载地址: https://github.com/ARMmbed/mbedtls/releases
- ssl和tls简明教程: http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html
- mbedtls官方说明文档: https://tls.mbed.org/high-level-design
Linux C++ 网络编程学习系列(7)——mbedtls编译使用的更多相关文章
- Linux C++ 网络编程学习系列(1)——端口复用实现
Linux C++ 网络编程学习系列(1)--端口复用实现 源码地址:https://github.com/whuwzp/linuxc/tree/master/portreuse 源码说明: serv ...
- Linux C++ 网络编程学习系列(6)——多路IO之epoll高级用法
poll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll_libevent 源码说明: server.cpp: 监听127. ...
- Linux C++ 网络编程学习系列(5)——多路IO之epoll边沿触发
多路IO之epoll边沿触发+非阻塞 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll_ET_LT_NOBLOCK_example 源码说 ...
- Linux C++ 网络编程学习系列(4)——多路IO之epoll基础
epoll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll 源码说明: server.cpp: 监听127.1:6666,功 ...
- Linux C++ 网络编程学习系列(3)——多路IO之poll实现
poll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/poll 源码说明: server.cpp: 监听127.1:6666,功能是 ...
- Linux C++ 网络编程学习系列(2)——多路IO之select实现
select实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/select 源码说明: server.cpp: 监听127.1:6666 ...
- Linux C网络编程学习笔记
Linux C网络编程总结报告 一.Linux C 网络编程知识介绍: 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端:(client) 在网络程序中, ...
- linux下网络编程学习——入门实例ZZ
http://www.cppblog.com/cuijixin/archive/2008/03/14/44480.html 是不是还对用c怎么实现网络编程感到神秘莫测阿,我们这里就要撕开它神秘的面纱, ...
- Linux下网络编程学习杂记
1.TCP/IP协议的体系结构包含四层:应用层(负责应用程序的网络服务,通过端口号识别各个不同的进程)->传输层(传输控制层协议TCP.用户数据报协议UDP.互联网控制消息协议ICMP)-> ...
随机推荐
- 深度学习归一化:BN、GN与FRN
在深度学习中,使用归一化层成为了很多网络的标配.最近,研究了不同的归一化层,如BN,GN和FRN.接下来,介绍一下这三种归一化算法. BN层 BN层是由谷歌提出的,其相关论文为<Batch No ...
- Pyinstaller通过spec文件打包py程序(多个py脚本)
Pyinstaller pyinstaller是python的一个第三方模块,使用它可以将python程序打包为可执行文件,实现打包后的程序在没有python环境的机器上也可以运行.pyinstall ...
- 如何使用Postman编写Testlink测试用例
Postman2Testlink 通过Postman快速操作testlink测试用例.测试套件.测试计划.添加关键词.添加自定义字段等等. 工具地址 https://github.com/liyinc ...
- Journal of Proteome Research | Down-Regulation of a Male-Specific H3K4 Demethylase, KDM5D, Impairs Cardiomyocyte Differentiation (男性特有的H3K4脱甲基酶基因(KDM5D)下调会损伤心肌细胞分化) | (解读人:徐宁)
文献名:Down-Regulation of a Male-Specific H3K4 Demethylase, KDM5D, Impairs Cardiomyocyte Differentiatio ...
- mvc+ef入门(三)
(1)新建一个DAL层用来放置Accountcontext.cs和Accountinitializer.新建一个models层用来归放sysuser,sysrole和sysuserrole,三个类.( ...
- 微信小程序开发-小程序之间的跳转
前几天开发微信小程序,其中有个需要联动宣传的业务,就是正在开发的小程序跳转到别的小程序去, 然后去看了下大家的做法与看法,总结下这小程序跳转之间应该注意到的几个问题 首先是跳转的方法, https:/ ...
- CERN Root与CLING
CERN Root on Arch Linux For WSL: 一个CLI才是本体的程序居然有图形启动界面,莫名的微妙感 接触到Root是在一个4chan上喷matlab的thread里.某anon ...
- BIT-Count of Range Sum
2019-12-17 18:56:56 问题描述: 问题求解: 本题个人感觉还是很有难度的,主要的难点在于如何将题目转化为bit计数问题. 首先构建一个presum数组,这个没有问题. 需要对于任意一 ...
- Spring04——Spring MVC 全解析
前文分别介绍了 Spring IOC 与 Spring AOP 的相关知识,本文将为各位大概带来 Spring MVC 的知识点.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 J ...
- IP协议的助手 —— ICMP 协议
IP协议的助手 —— ICMP 协议 IP协议的助手 —— ICMP 协议 ping 是基于 ICMP 协议工作的,所以要明白 ping 的工作,首先我们先来熟悉 ICMP 协议. ICMP 是什么? ...