libcurl编译

本文介绍 libcurl 在 centos 7 和 debian 7 系统中的编译及使用。

一、准备工作

下载:

1
2
https://github.com/curl/curl/releases/tag/curl-7_66_0
压缩文件名:curl-7.66.0.tar.bz2

二、编译

libcurl 编译步骤如下:

1
2
3
4
5
tar xf curl-7.66.0.tar.bz2
cd curl-7.66.0
./configure --prefix=/home/highwayx/bin/curl --with-libssh2
make -j
make install

注:ssl默认已使能,这里需要指定libssh2,以便使能 sftp 协议。

32位 debian 7 编译:

1
2
3
./configure --prefix=/root/bin/curl --with-libssh2 --disable-ldap  --disable-ldaps --without-librtmp
make -j
make install

三、使用

curl 使用:

1
2
3
4
5
6
查看版本及支持的协议:
$ ./curl --version
curl 7.66.0 (x86_64-pc-linux-gnu) libcurl/7.66.0 OpenSSL/1.0.2k-fips zlib/1.2.7 libssh2/1.8.0
Release-Date: 2019-09-11
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets

如果系统存在多个 curl 库,则会提示:

1
2
3
4
5
6
7
$ ./curl --version
curl 7.66.0 (x86_64-pc-linux-gnu) libcurl/7.29.0 NSS/3.36 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Release-Date: 2019-09-11
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets
WARNING: curl and libcurl versions do not match. Functionality may be affected.
./curl: symbol lookup error: ./curl: undefined symbol: curl_mime_free

说明:对比上述两个版本信息,发现虽然协议都支持 sftp,但后者特征(Features)处没有HTTPS-proxy,在运行时使用 sftp 协议,提示该协议不支持。

四、问题及解决

编译过程中提示:

1
2
3rdparty/curl_x64/lib/libcurl.a(libcurl_la-libssh2.o): In function `ssh_block2waitfor':
libssh2.c:(.text+0x268): undefined reference to `libssh2_session_block_directions'

在链接时添加-lssh2库。

1
configure: error: libSSH libs and/or directories were not found where specified!

编译过程中提示:

1
2
3
3rdparty/curl_x64/lib/libcurl.a(libcurl_la-openssl.o): In function `ossl_recv':
openssl.c:(.text+0x3bc): undefined reference to `SSL_read'
openssl.c:(.text+0x3ee): undefined reference to `SSL_get_error'

在链接时添加-lssl库。

运行过程中提示:

1
Unsupported protocol

重新编译,加上--with-libssh2,该库在目录/lib64/中。

李迟 2021.8.28 周六 凌晨