libcurl是一个免费且易于使用的客户端URL传输库,支持DICT、FILE、FTP、FTPS、Gopher、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMTP、SMTPS、Telnet和TFTP。libcurl支持SSL证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP表单的上传,代理,Cookie,用户+密码验证(Basic,Digest,NTLM,Negotiate,Kerberos),文件传输恢复,http代理隧道等等!
libcurl是高度可移植的,它在许多平台上构建和工作相同,包括Solaris,NetBSD,FreeBSD,OpenBSD,Darwin,HPUX,IRIX,AIX,Tru64,Linux,UnixWare,Hurd,Windows,Amiga,OS/2,BeOS,Mac OS X,Ultrix,QNX,OpenVMS,RISC OS,Novell NetWare,DOS等等。
在当前进行的一个项目中,需要实现HTTP/HTTPS数据传输通信,且需要跨平台的代码编译运行。libcur为实现项目功能、代码复用和跨平台运行提供了稳定简单的解决方案。
平台
系统:Windows10 x64、Ubuntu16 x64
工具:Visual Studio 2017、Linux Android NDK
openssl:openssl-1.1.1b
zlib:zlib-1.2.11
libcurl:curl-7.65.3
Linux系统Android NDK编译libcurl
编译openssl
在openssl源码目录提供了在各个平台下编译的说明文本:
NOTES.ANDROID介绍了编译Android系统中openssl库的方法。
1、编译armeabi-v7a版本
环境变量设置:
生成Makefile:
# ./Configure android-arm threads shared --prefix=/home/workspace/android-plarfrom
编译:
# make
# make install
编译完成后在安装路径下可以找到目标文件:
2、编译arm64-v8a版本
环境变量设置:
生成Makefile:
# ./Configure android-arm64 threads shared --prefix=/home/workspace/android-plarfrom/openssl_64/target
编译:
# make
# make install
编译完成后在安装路径下可以找到目标文件:
编译zlib
创建jni目录,拷贝zlib源码到jni目录下,并在目录下创建Android.mk:
LOCAL_PATH:= $(call my-dir)
#########################
# Build the libzip library
include $(CLEAR_VARS)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE:= zlib
LOCAL_SRC_FILES:= adler32.c compress.c crc32.c \
deflate.c gzclose.c gzlib.c gzread.c gzwrite.c \
infback.c inffast.c inflate.c inftrees.c \
trees.c uncompr.c zutil.c
include $(BUILD_STATIC_LIBRARY)
执行ndk-build编译:
# ndk-build
生成目标文件:
编译libcurl
libcur默认只支持HTTP通信,若要编译支持HTTPS的版本,需要依赖openssl和zlib。
1、创建jni目录。
2、将libcurl源码拷贝到jni目录下。
3、准备curl_config.h,并将其拷贝到源码lib目录下。
curl_config.h生成有两种方式:
(1)执行命令:
# ./configure --host=arm-linux-androideabi --with-ssl=PATH
NOTE:PATH一般应配置为openssl的ssl目录。
(2)手动生成.
4、修改curl_config.h,打开宏定义HAVE_LIBSSL、HAVE_OPENSSL_CRYPTO_H、HAVE_OPENSSL_ERR_H、HAVE_OPENSSL_PEM_H、HAVE_OPENSSL_PKCS12_H、HAVE_OPENSSL_RSA_H、HAVE_OPENSSL_SSL_H、HAVE_OPENSSL_X509_H、USE_OPENSSL、USE_SSLEAY,掉注释掉宏定义HAVE_MALLOC_H和HAVE_IOCTL。
curl_config.h
/* lib/curl_config.h. Generated from curl_config.h.in by configure. */
/* lib/curl_config.h.in. Generated from configure.ac by autoheader. */
/* when building libcurl itself */
/* #undef BUILDING_LIBCURL */
/* Location of default ca bundle */
/* #undef CURL_CA_BUNDLE */
/* Location of default ca path */
/* #undef CURL_CA_PATH */
/* to disable cookies support */
/* #undef CURL_DISABLE_COOKIES */
/* to disable cryptographic authentication */
/* #undef CURL_DISABLE_CRYPTO_AUTH */
/* to disable DICT */
/* #undef CURL_DISABLE_DICT */
/* to disable FILE */
/* #undef CURL_DISABLE_FILE */
/* to disable FTP */
#define CURL_DISABLE_FTP 1
/* to disable HTTP */
/* #undef CURL_DISABLE_HTTP */
/* to disable IMAP */
#define CURL_DISABLE_IMAP 1
/* to disable LDAP */
#define CURL_DISABLE_LDAP 1
/* to disable LDAPS */
#define CURL_DISABLE_LDAPS 1
/* to disable POP3 */
#define CURL_DISABLE_POP3 1
/* to disable proxies */
/* #undef CURL_DISABLE_PROXY */
/* to disable RTSP */
#define CURL_DISABLE_RTSP 1
/* to disable SMTP */
#define CURL_DISABLE_SMTP 1
/* to disable TELNET */
#define CURL_DISABLE_TELNET 1
/* to disable TFTP */
#define CURL_DISABLE_TFTP 1
/* to disable verbose strings */
/* #undef CURL_DISABLE_VERBOSE_STRINGS */
/* to make a symbol visible */
/* #undef CURL_EXTERN_SYMBOL */
/* to enable hidden symbols */
/* #undef CURL_HIDDEN_SYMBOLS */
/* W$ LDAP with non-W$ compiler */
/* #undef CURL_LDAP_HYBRID */
/* Use W$ LDAP implementation */
/* #undef CURL_LDAP_WIN */
/* when not building a shared library */
/* #undef CURL_STATICLIB */
/* your Entropy Gathering Daemon socket pathname */
/* #undef EGD_SOCKET */
/* Define if you want to enable IPv6 support */
/* #undef ENABLE_IPV6 */
/* Define to the type qualifier of arg 1 for getnameinfo. */
#define GETNAMEINFO_QUAL_ARG1 const
/* Define to the type of arg 1 for getnameinfo. */
#define GETNAMEINFO_TYPE_ARG1 struct sockaddr *
/* Define to the type of arg 2 for getnameinfo. */
#define GETNAMEINFO_TYPE_ARG2 socklen_t
/* Define to the type of args 4 and 6 for getnameinfo. */
#define GETNAMEINFO_TYPE_ARG46 size_t
/* Define to the type of arg 7 for getnameinfo. */
#define GETNAMEINFO_TYPE_ARG7 int
/* Specifies the number of arguments to getservbyport_r */
/* #undef GETSERVBYPORT_R_ARGS */
/* Specifies the size of the buffer to pass to getservbyport_r */
/* #undef GETSERVBYPORT_R_BUFSIZE */
/* Define to 1 if you have the alarm function. */
#define HAVE_ALARM 1
/* Define to 1 if you have the <alloca.h> header file. */
#define HAVE_ALLOCA_H 1
/* Define to 1 if you have the <arpa/inet.h> header file. */
#define HAVE_ARPA_INET_H 1
/* Define to 1 if you have the <arpa/tftp.h> header file. */
/* #undef HAVE_ARPA_TFTP_H */
/* Define to 1 if you have the <assert.h> header file. */
#define HAVE_ASSERT_H 1
/* Define to 1 if you have the basename function. */
#define HAVE_BASENAME 1
/* Define to 1 if bool is an available type. */
#define HAVE_BOOL_T 1
/* Define to 1 if you have the clock_gettime function and monotonic timer. */
#define HAVE_CLOCK_GETTIME_MONOTONIC 1
/* Define to 1 if you have the closesocket function. */
/* #undef HAVE_CLOSESOCKET */
/* Define to 1 if you have the CloseSocket camel case function. */
/* #undef HAVE_CLOSESOCKET_CAMEL */
/* Define to 1 if you have the connect function. */
#define HAVE_CONNECT 1
/* Define to 1 if you have the `CRYPTO_cleanup_all_ex_data' function. */
/* #undef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA */
/* Define to 1 if you have the <crypto.h> header file. */
/* #undef HAVE_CRYPTO_H */
/* Define to 1 if you have the <des.h> header file. */
/* #undef HAVE_DES_H */
/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
/* Define to 1 if you have the `ENGINE_cleanup' function. */
/* #undef HAVE_ENGINE_CLEANUP */
/* Define to 1 if you have the `ENGINE_load_builtin_engines' function. */
/* #undef HAVE_ENGINE_LOAD_BUILTIN_ENGINES */
/* Define to 1 if you have the <errno.h> header file. */
#define HAVE_ERRNO_H 1
/* Define to 1 if you have the <err.h> header file. */
/* #undef HAVE_ERR_H */
/* Define to 1 if you have the fcntl function. */
#define HAVE_FCNTL 1
/* Define to 1 if you have the <fcntl.h> header file. */
#define HAVE_FCNTL_H 1
/* Define to 1 if you have a working fcntl O_NONBLOCK function. */
#define HAVE_FCNTL_O_NONBLOCK 1
/* Define to 1 if you have the fdopen function. */
#define HAVE_FDOPEN 1
/* Define to 1 if you have the `fork' function. */
#define HAVE_FORK 1
/* Define to 1 if you have the freeaddrinfo function. */
#defi