Compiling external C++ library for use with iOS project Compiling external C++ library for use with iOS project ios ios

Compiling external C++ library for use with iOS project


So I've used many a 3rd party C++ library in my iOS projects. There are different strategies people use for this. As some have already cited, you can include the code within the project directly, build the static lib with Xcode, or build it command line. In the case of cross platform C++ libs which use the GNU configure and build system, I prefer command line. You only need to build it once and you only have to revisit it if you need to update the version or add a new architecture slice.

The generalized approach you want is:

  • Figure out the right configure arguments to use to build each slice. Typically, you only need to focus on getting one of the arm as well as i386 working. The rest are easy one you have this done. In some cases, you actually need to modify the configure file to add the host or make some other adjustments.

  • Once you can build all slices, you want to run lipo to build a fat binary.

The best way then to deal with this is create a build script which will do all the work for you. This way, it's easier to redo. More importantly, you can reuse the script or permute it to build other external libs.

There are many ways you can build the script. Here is one. I happen to have several variations of this type of script. This script was used to build cURL. It more or less worked for presage with very little mod (ie. change curl to presage). Note I didn't test it in Xcode (ie. linking it and running it). I did find that I had to disable sqlite, else it built tool items which don't build right. If you need it, you can figure that part out.

There are many ways you could make it more slick. For example using an array to store all the architectures. This is just brute force.

The key points of the script are:

  1. Getting the latest SDK
  2. Building each slice
  3. Then running lipo

Note that it should work out of the box, however, YMMV. Be prepared to have to debug it if necessary. For example, I haven't confirmed the host type, but generally that is what I've always used. You want to put this at the directory for presage (same directory where configure). When it is done, all architectures are in the output directory. The universal lib is in the presage directory.

Also remember it is your responsibility to properly link in the universal lib as well as have the header files search path defined properly.

#!/bin/bashPLATFORMPATH="/Applications/Xcode.app/Contents/Developer/Platforms"TOOLSPATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"export IPHONEOS_DEPLOYMENT_TARGET="8.0"pwd=`pwd`findLatestSDKVersion(){    sdks=`ls $PLATFORMPATH/$1.platform/Developer/SDKs`    arr=()    for sdk in $sdks    do       arr[${#arr[@]}]=$sdk    done    # Last item will be the current SDK, since it is alpha ordered    count=${#arr[@]}    if [ $count -gt 0 ]; then       sdk=${arr[$count-1]:${#1}}       num=`expr ${#sdk}-4`       SDKVERSION=${sdk:0:$num}    else       SDKVERSION="8.0"    fi}buildit(){    target=$1    hosttarget=$1    platform=$2    if [[ $hosttarget == "x86_64" ]]; then        hostarget="i386"    elif [[ $hosttarget == "arm64" ]]; then        hosttarget="arm"    fi    export CC="$(xcrun -sdk iphoneos -find clang)"    export CPP="$CC -E"    export CFLAGS="-arch ${target} -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -miphoneos-version-min=$SDKVERSION"    export AR=$(xcrun -sdk iphoneos -find ar)    export RANLIB=$(xcrun -sdk iphoneos -find ranlib)    export CPPFLAGS="-arch ${target}  -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -miphoneos-version-min=$SDKVERSION"    export LDFLAGS="-arch ${target} -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk"    mkdir -p $pwd/output/$target     ./configure --prefix="$pwd/output/$target" --disable-shared --disable-sqlite --host=$hosttarget-apple-darwin    make clean    make    make install}findLatestSDKVersion iPhoneOSbuildit armv7 iPhoneOSbuildit armv7s iPhoneOSbuildit arm64 iPhoneOSbuildit i386 iPhoneSimulatorbuildit x86_64 iPhoneSimulatorLIPO=$(xcrun -sdk iphoneos -find lipo)$LIPO -create $pwd/output/armv7/lib/libpresage.a  $pwd/output/armv7s/lib/libpresage.a $pwd/output/arm64/lib/libpresage.a $pwd/output/x86_64/lib/libpresage.a $pwd/output/i386/lib/libpresage.a -output libpresage.a


Considering that you are new with C++ libraries, I guess you will need to do a bit more research.

However, I will try to outline some steps things you need to take into consideration :

  • you need to make sure you compile for the same architecture both the static library (.a) and the project
  • from your error , you need to compile your static library for i386 OR change your project to x86_64 ( the difference between these architectures is a bit more complex, but for now let's say that i386 means desktop 32 bit while x86_64 means desktop 64 bit)
  • arm architectures are for iPhone , not for your MacOS (that's why it fails to find libraries with arm architecture inside the MacOSX folder) !

There are multiple ways to approach these issues .

For the first one I would suggest to include into your workspace the static library, and add it as dependency to your build target . For this you need to understand XCode builds.

I'm guessing that you actually are trying to make a phone application, so for the 3rd option you need to configure your g++ build to look into the iPhoneSDK from XCode when linking arm targets (look after iPhoneOS.platform) for this.

Making an arm build will only work on iPhones . If you want it to work on simulator , you will need to link your static library to libraries inside the iPhoneSimulator.platform.

If you want your static library to work for both iPhones and iPhone simulator, you will need to make a fat lib (basically a library containing symbols for both platforms)

If you are missing these platforms, you can download them from XCode (but I believe they are there)

As you can see, things are going to get more and more complex along the way, so I strongly recommend to use XCode for compiling the static library (it is still doable with g++ thou).

I believe the following concepts you would be useful to research upon :

  • arm, x86 , x86_64
  • static library
  • static linkage
  • fat lib (universal library)
  • XCode workspace with multiple projects

Hope this helps :).


Here is what worked for me in Xcode 9 for iOS devices (iPhone X):
1) Compile dylib with these flags set as the following:
a) "Installation Directory": @executable_path/Frameworks b) "Runpath Search Path": @executable_path/Frameworks
See the picture bellow:
dylib settings in Xcode 9

2) In the Xcode project where the dylib is used/linked:
a) "Runpath Search Path":
@executable_path/Frameworks
b) In "Build Phase->Embed Libraries" make sure you select "Destination" as "Executables" and Subpath as "Frameworks", "Code sign on copy" checked:
Setting the the linking iOS app

This method is tested and used with Xcode 9.2 and iPhone X.

David


matomo