在github上面发现的一个很棒的脚本,非常值得学习的。我用的是最新的curl-7.48.0源码编译的,curl-7.48.0.tar
libcurl for iOS
Build libcurl for iOS development. This script will generate static library for armv7 armv7s arm64 i386 and x86_64. Bitcode support. OpenSSL and Darwin native ssl support.
Script only, please download libcurl from here: http://curl.haxx.se/download.htmlTested Xcode 7.2.1(7C1002) on OSX 10.11.3 Tested curl 7.44.0 and 7.47.1
Usage
curl -O http://curl.haxx.se/download/curl-7.47.1.tar.gz tar xf curl-7.47.1.tar.gz cd curl-7.47.1 curl https://raw.githubusercontent.com/sinofool/build-libcurl-ios/master/build_libcurl_dist.sh |bash
......
build_libcurl_dist.sh的内容如下:
#!/bin/bash
export DEVROOT=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
DFT_DIST_DIR=${HOME}/Desktop/libcurl-ios-dist
DIST_DIR=${DIST_DIR:-$DFT_DIST_DIR}
function check_curl_ver() {
echo "#include \"include/curl/curlver.h\"
#if LIBCURL_VERSION_MAJOR < 7 || LIBCURL_VERSION_MINOR < 40
#error Required curl 7.40.0+; See http://curl.haxx.se/docs/adv_20150108A.html
#endif"|gcc -c -o /dev/null -xc -||exit 9
}
function build_for_arch() {
ARCH=$1
HOST=$2
SYSROOT=$3
PREFIX=$4
IPHONEOS_DEPLOYMENT_TARGET="6.0"
export PATH="${DEVROOT}/usr/bin/:${PATH}"
export CFLAGS="-arch ${ARCH} -pipe -Os -gdwarf-2 -isysroot ${SYSROOT} -miphoneos-version-min=${IPHONEOS_DEPLOYMENT_TARGET} -fembed-bitcode"
export LDFLAGS="-arch ${ARCH} -isysroot ${SYSROOT}"
./configure --disable-shared --enable-static ${SSL_FLAG} --host="${HOST}" --prefix=${PREFIX} && make -j8 && make install
}
if [ "$1" == "openssl" ]
then
if [ ! -d ${HOME}/Desktop/openssl-ios-dist ]
then
echo "Please use https://github.com/sinofool/build-openssl-ios/ to build OpenSSL for iOS first"
exit 8
fi
export SSL_FLAG=--with-ssl=${HOME}/Desktop/openssl-ios-dist
else
check_curl_ver
export SSL_FLAG=--with-darwinssl
fi
TMP_DIR=/tmp/build_libcurl_$$
build_for_arch i386 i386-apple-darwin /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ${TMP_DIR}/i386 || exit 1
build_for_arch x86_64 x86_64-apple-darwin /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ${TMP_DIR}/x86_64 || exit 2
build_for_arch arm64 arm-apple-darwin /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk ${TMP_DIR}/arm64 || exit 3
build_for_arch armv7s armv7s-apple-darwin /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk ${TMP_DIR}/armv7s || exit 4
build_for_arch armv7 armv7-apple-darwin /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk ${TMP_DIR}/armv7 || exit 5
mkdir -p ${TMP_DIR}/lib/
${DEVROOT}/usr/bin/lipo \
-arch i386 ${TMP_DIR}/i386/lib/libcurl.a \
-arch x86_64 ${TMP_DIR}/x86_64/lib/libcurl.a \
-arch armv7 ${TMP_DIR}/armv7/lib/libcurl.a \
-arch armv7s ${TMP_DIR}/armv7s/lib/libcurl.a \
-arch arm64 ${TMP_DIR}/arm64/lib/libcurl.a \
-output ${TMP_DIR}/lib/libcurl.a -create
cp -r ${TMP_DIR}/armv7s/include ${TMP_DIR}/
curl -O https://raw.githubusercontent.com/sinofool/build-libcurl-ios/master/patch-include.patch
patch ${TMP_DIR}/include/curl/curlbuild.h < patch-include.patch
mkdir -p ${DIST_DIR}
cp -r ${TMP_DIR}/include ${TMP_DIR}/lib ${DIST_DIR}
build_libcurl_dist.sh下载地址:build_libcurl_dist.sh (该文件备用,防止上面curl失败)
桌面会生成一个目录libcurl-ios-dist,里面包含了我们需要头文件和.a静态库(该库是几个平台合并在一起的)
补充:创建OC工程使用libcurl.a的时候切记一点,必须要添加zlib库,不然会提示未定义的符号之类的错误。libz.dylib。 补充二:后续用在Swift工程中,报这个错误:ld: library not found for -lcurl,后来在Libary Search Paths添加桌面目录下面的lib目录即可。如我的:/Users/leehom/Desktop/libcurl-ios-dist/lib

测试代码:
void test_post(char* url,char* data) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
//www.baidu.com/#wd=java
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
test_post("http://www.baidu.com", "wd=java");
}
详情请移步:xcode7.2引用libcurl库出现”_inflate”, referenced from:错误解决方法
OpenSSL
The script link with native ssl by default (--with-darwinssl). To use OpenSSL, use https://github.com/sinofool/build-openssl-ios/ to build OpenSSL for iOS first, in curl source directory run: curl https://raw.githubusercontent.com/sinofool/build-libcurl-ios/master/build_libcurl_dist.sh openssl |bash
Binary
You can find a prebuild binary (with OpenSSL) here: https://sinofool.net/dl/libcurl-ios-dist.tar.bz2
Double check the binary file before use: SHA1: 993c9bb75d798a886749e7801d5f54c494dbf6fb libcurl-ios-dist.tar.bz2
GnuPG: (My Key ID: 9BE18853) https://sinofool.net/dl/libcurl-ios-dist.tar.bz2.sig
附件:build-libcurl-ios-master
昨天升级iOS程序,顺便升级了依赖到的两个库,OpenSSL和cURL。 升级了版本,增加了新的iPhone5s的64位CPU的支持。
I have updated the dependency libraries of my iPhone app, OpenSSL and cURL. Added support of iPhone5s new 64bit arm64 CPU. Upgraded to latest version.
Approach
交叉编译这两个库的关键是两个参数:-isysroot和-miphoneos-version-min cURL是拼上不同的参数实现的,OpenSSL已经内建有iphoneos-cross支持,修改一些参数来支持arm64和模拟器。
The key of cross compiling are two parameters: -isysroot and -miphoneos-version-min cURL is configured using parameters. While OpenSSL has a build-in target called iphoneos-cross, I added 64 bit support based on it.
Code
这两个项目的代码提交在了GitHub,目前用iOS SDK7.1在MacOSX 10.8验证通过. Here are two projects on GitHub, tested on iOS SDK 7.1 and MacOSX 10.8 .https://github.com/sinofool/build-openssl-ioshttps://github.com/sinofool/build-libcurl-ios
Usage
这两个脚本不需要git clone再使用,下载好OpenSSL和cURL的源代码并解压缩,直接运行github上的脚本,就会编译好放在桌面上。 It is not necessary clone the code locally, download the sources from OpenSSL and cURL official website. Run following scripts, results will be on the desktop.
curl -O http://www.openssl.org/source/openssl-1.0.1f.tar.gz
tar xf openssl-1.0.1f.tar.gz
cd openssl-1.0.1f
curl https://raw.githubusercontent.com/sinofool/build-openssl-ios/master/build_openssl_dist.sh |bash
cURL也是一样:
curl -O http://curl.haxx.se/download/curl-7.35.0.tar.gz tar xf curl-7.35.0.tar.gz cd curl-7.35.0
curl https://raw.githubusercontent.com/sinofool/build-libcurl-ios/master/build_libcurl_dist.sh
- 文章2313
- 用户1336
- 访客11746226
登高望远,心也年轻。