Using libcurl on iOS 5 as an alternative to NSURLConnection

…衆ロ難τιáo~ 提交于 2019-12-02 19:38:25
Bob

This worked for me with the latest SDK:

#!/bin/sh 
export SDK=5.0

buildit()
{
    target=$1
    platform=$2

    export CC=/Developer/Platforms/${platform}.platform/Developer/usr/bin/gcc
    export CFLAGS="-arch ${target} -isysroot /Developer/Platforms/${platform}.platform/Developer/SDKs/${platform}${SDK}.sdk"
    export CPP="/Developer/Platforms/${platform}.platform/Developer/usr/bin/llvm-cpp-4.2"
    export AR=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ar
    export RANLIB=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ranlib

    ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap \
            --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a

Important Update for people getting: C compiler cannot create executables
Make sure you install the command line XCode tools...

From XCode menu select: Preferences --> downloads --> Command Line Tools

...and hit "Install" if not installed.

The issue with the above script is that it works with older versions of XCode. If you installed the latest XCode, the location of all the compiler tools will be different. I've changed the script to support both old and new XCode versions, and have some error checking built-in. Does this help?

Below -- I assume SDK version 5.1 -- you may need to modify that...

#!/bin/bash

set -e

export SDK=5.1

checkExists() {

    if [ ! -e $1 ]
    then
        echo "Didn't find: $1 -- try to locate parts of this to see how to fix the path"
        exit 1
    else 
        echo "using $1"
    fi

}

buildit() {

    target=$1
    platform=$2

    root="/Applications/Xcode.app/Contents/Developer/Platforms/${platform}.platform/Developer"
    oldRoot="/Developer/Platforms/${platform}.platform/Developer"

    if [ ! -d "${root}" ]
    then
        root="${oldRoot}"
    fi

    if [ ! -d "${root}" ]
    then
        echo " "
        echo "Oopsie.  You don't have an iOS SDK root in either of these locations: "
        echo "   ${root} "
        echo "   ${oldRoot}"
        echo " "
        echo "If you have 'locate' enabled, you might find where you have it installed with:"
        echo "   locate iPhoneOS.platform | grep -v 'iPhoneOS.platform/'"
        echo " "
        echo "and alter the 'root' variable in the script -- or install XCode if you can't find it... "
        echo " "
        exit 1
    fi

    export CC="${root}/usr/bin/gcc"
    export CFLAGS="-arch ${target} -isysroot ${root}/SDKs/${platform}${SDK}.sdk"
    export CPP="${root}/usr/bin/llvm-cpp-4.2"
    export AR="${root}/usr/bin/ar"
    export RANLIB="${root}/usr/bin/ranlib"

    checkExists ${CC}
    checkExists ${root}/SDKs/${platform}${SDK}.sdk
    checkExists ${CPP}
    checkExists ${AR}
    checkExists ${RANLIB}

    ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a
Guilhem BERTHALON

Execute script with sudo :

$ sudo library.sh 

my script :

#!/bin/sh 
export SDK=5.0

buildit()
{
    target=$1
    platform=$2

    export CC=/Developer/Platforms/${platform}.platform/Developer/usr/bin/gcc
    export CFLAGS="-arch ${target} -isysroot /Developer/Platforms/${platform}.platform/Developer/SDKs/${platform}${SDK}.sdk"
    export CPP="/Developer/Platforms/${platform}.platform/Developer/usr/bin/llvm-cpp-4.2"
    export AR=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ar
    export RANLIB=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ranlib

    sudo ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --    without-ldap --disable-ldap \ --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a

The problem is:

C compiler cannot create executables

The reason:

Your C compiler for iOS, arm-apple-darwin9-gcc, is a cross-compiler: it runs on a PC, but generates code for iDevices. Thus, it can't use the computer's native programming resources (libraries, header files). You have to tell it where to find iOS's own headers and libraries. You can do it by passing it the -isysroot=/path/to/iOS_SDK/ option. For example, the following won't work out of the box:

/path/to/arm-apple-darwin10-gcc hello.c -o hello

But the following will:

/path/to/arm-apple-darwin10-gcc -isysroot /Developer/Platforms/iPhoneOS.platform/SDKs/iPhoneOS5.0.1.sdk hello.c -o hello

I don't use Xcode, neither do I have a Mac, so I don't know the exact location of the iOS toolchain sysroot, but it might be something like the one in the above command.

And how to use all this with configure? Well, you have 3 compilation stages: preprocessing, compiling and linking. SO you have to tell the preprocessor and the compiler where can it find the headers, and the linker where can it find the libraries. (Particularily, you have to forget to tell the preprocessor where your sysroot is). So do something like this:

export CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
export CFLAGS="-arch armv7 -isysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk"
export CPPFLAGS="-isysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk"
export LDFLAGS="-arch armv7 -isysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk"
export CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-cpp-4.2
export AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=arm-apple-darwin10 --build=arm-apple-darwin10
make
sudo make install

"I'm still getting the same error, "C compiler cannot create executables" :| " Just run ./configure as root (sudo ./configure ....)

I've set up a project on GitHub that builds curl (for HTTP only) with and without SSL (GnuTLS and OpenSSL). Left many comments in place so should be easy enough for anyone to modify according to his particular needs.

This script worked for me with Xcode 4.4.1 environment (excuse my almost non-existent sh skills):

#!/bin/sh

#run with sudo

export OSSDK="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/"
export SIMSDK="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/"
export SDKROOT=$OSSDK
export VER=5.1

buildit()
{
    target=$1
    platform=$2

    export CC="${root}/usr/bin/llvm-gcc-4.2"
    export CFLAGS="-arch ${target} -isysroot=${PLATFORM}/${SDKROOT}/${PLATFORM}${SDK}.sdk"
    export CPPFLAGS="-isysroot=${SDKROOT}/${platform}${SDK}.sdk"
    export LDFLAGS="-arch ${target} -isysroot=${SDKROOT}/${platform}${SDK}.sdk"
    export CPP="${root}/usr/bin/llvm-cpp-4.2"
    export AR="${root}/usr/bin/ar"    
    export RANLIB="${root}/usr/bin/ranlib"

    sudo ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=arm-apple-darwin10 --build=arm-apple-darwin10

    sudo make clean
    sudo make
    sudo $AR rv libcurl.${target}.a lib/*.o
}

# Run once for armv6 & armv7, then for i386. Comment lines alternatively, as explained below.

# Run for armv6 & armv7 by changing line 6 to "export SDKROOT=$OSSDK". Comment line with "buildit i386..."
#buildit armv6 iPhoneOS
#buildit armv7 iPhoneOS

# Run for i386 by changing line 6 to "export SDKROOT=$SIMSDK". Comment line with buildit "armv6..." and "buildit armv7..."
buildit i386 iPhoneSimulator

sudo lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!