iMX6UL开发板移植libcurl库

前言

libcurl 是一个功能全面的客户端 URL 传输库,支持常见的各种传输协议(官方介绍),并且具有高可移植性,命令行工具 curl 就是使用的这个库。在嵌入式开发板上,有 http/https 等使用需求时,可以考虑使用 libcurl。使用 libcurl 所需的库文件和头文件可以通过编译curl源码得到。

curl 源码下载地址:https://curl.se/download.html。
我使用的是:curl-7.79.1.tar.gz

编译流程

./configure ...
make
make install

官方提供了一个编译和安装的文档,可以参考:how to install curl and libcurl。

curl 源码的 configure 工具有很多可选配置项。可以通过 ./configure --help 查看。我在配置时参考了这篇博客:libcurl源码编译及使用。

简单编译(不支持 HTTPS)

如果只需要http,不需要支持https,可以使用如下配置:

./configure CC=arm-none-linux-gnueabi-gcc \
--prefix=/home/sy/curl-7.79.1/out \
--disable-static \
--enable-shared \
--without-libidn \
--without-ssl \
--without-librtmp \
--without-gnutls \
--without-nss \
--without-libssh2 \
--without-zlib \
--without-winidn \
--disable-rtsp \
--disable-ldap \
--disable-ldaps \
--disable-ipv6 \
--host=arm-linux
  • CC 指定交叉编译使用的 gcc ;
  • prefix用于指定 make install 的安装目录;
  • 使用 --disable-static --enable-shared--disable-shared --enable-static 指定输出动态库(.so)或静态库(.a);

支持 HTTPS 编译

要支持 https,重点就是 ./configure 要添加 --with-ssl 选项,但这个选项依赖 openssl 库,所以还需要先交叉编译 openssl 库。

1、编译 openssl

openssl 库源码下载地址:https://www.openssl.org/source/。
我这里使用的是:openssl-1.1.1l.tar.gz
交叉编译配置:

./Configure shared no-asm \
--prefix=/home/sy/openssl-1.1.1l/out \
--cross-compile-prefix=arm-none-linux-gnueabi- \
linux-armv4

编译:

make && make install

编译完成后,输出目录如下:
iMX6UL开发板移植libcurl库_第1张图片

2、编译 curl

这里参考了上面提到的官方文档里的步骤,配置如下:

# step 1
export PKG_CONFIG_PATH=/home/sy/openssl-1.1.1l/out/lib/pkgconfig
# step 2
./configure CC=arm-none-linux-gnueabi-gcc \
--prefix=/home/sy/curl-7.79.1/out \
--disable-static \
--enable-shared \
--without-libidn \
--with-ssl \
--without-librtmp \
--without-gnutls \
--without-nss \
--without-libssh2 \
--without-zlib \
--without-winidn \
--disable-rtsp \
--disable-ldap \
--disable-ldaps \
--disable-ipv6 \
--host=arm-linux

编译:

make && make install

编译安装的目录结构如下:
iMX6UL开发板移植libcurl库_第2张图片

  • bin 目录下是可执行的 curl 命令行工具;
  • include 目录是使用 libcurl 库需要的头文件;
  • lib 目录下就是 libcurl 库的静态库(.a)或动态库(.so);
  • share 目录是 man 手册文档。

移植和验证

因为我这里配置的动态链接,所以需要将 openssl 和 curl 编译输出的 lib 目录下的内容都拷贝到开发板上,比如开发板的 /opt 目录下,然后在库加载路径环境变量中添加 openssl 和 libcurl 库的路径。为了方便测试,我把 bin 目录也拷贝了,相当于移植了 openssl 和 curl 两个命令行工具。

export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/opt/curl/lib:/opt/openssl/lib
export PATH=$PATH:/opt/curl/bin:/opt/openssl/bin

可以通过 curl -V 命令来查看 curl 的信息:

[root@sy-iMX6UL]# curl -V
curl 7.79.1 (arm-unknown-linux-gnu) libcurl/7.79.1 OpenSSL/1.1.1l
Release-Date: 2021-09-22
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS HSTS HTTPS-proxy Largefile NTLM NTLM_WB SSL TLS-SRP UnixSockets

通过以上输出可以看到 curl 使用了 OpenSSL/1.1.1l 并支持 https 协议。

附 libcurl API 文档

The libcurl API
Easy interface overview
multi interface overview

你可能感兴趣的:(嵌入式学习笔记,curl,交叉编译,iMX6UL)