Need some help binding a Swift library.

Hello. I am trying to integrate a Objective C library, that has some swift code in it, in my Xamarin.iOS project. AFAIK, there is no official support for binding swift libraries. I get the following linker errors on building my project

ld : warning : Could not find or use auto-linked library 'swiftFoundation'
ld : warning : Could not find or use auto-linked library 'swiftDarwin'
ld : warning : Could not find or use auto-linked library 'swiftCoreFoundation'
ld : warning : Could not find or use auto-linked library 'swiftCore'
ld : warning : Could not find or use auto-linked library 'swiftCoreGraphics'
ld : warning : Could not find or use auto-linked library 'swiftObjectiveC'
ld : warning : Could not find or use auto-linked library 'swiftDispatch'
ld : warning : Could not find or use auto-linked library 'swiftSwiftOnoneSupport'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftFoundation'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftDarwin'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftCoreFoundation'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftCore'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftCoreGraphics'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftObjectiveC'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftDispatch'
error MT5209 : Native linking error : warning: Could not find or use auto-linked library 'swiftSwiftOnoneSupport'
So, I added this package via nuget. Now, I can see that it is supposed to run a bunch of targets during the build process. But in my case, none of the targets run, and the build log looks exactly the same as without this package included - with the same linker errors.

So, I am using this nuget package which looks like it might help me -> h ttps://github.com/Flash3001/Xamarin.Swift. This project defines some build targets that link the appropriate version of swift frameworks to your project. At least that's what I could make out of it. I am no expert on building and linking process. But none of the build targets defined in that project -> h ttps://github.com/Flash3001/Xamarin.Swift/blob/master/SwiftSupport/build/SwiftSupport-ios.targets run when I build my project after including it.

Any help?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Check Rules for Swift dependencies inclusion , maybe helpful .

    • If you project have any Swift dependency

    • and you minimum target OS version is lower than OS 12.2, macOS 10.14.4 and tvOS 12.2

    • or those libraries were build using a version of Swift lower than Swift 5

  • itwentyitwenty Member

    My project meets the requirements for inclusion.
    1. It has a swift dependency (The library I am trying to bind has swift code in it)
    2. Minimum target OS version is iOS 9.0
    3. The library I am trying to bind uses Swift >5. But this last condition is an OR. So the previous two are still being met :)

  • JosueYerayJulinFerreiroJosueYerayJulinFerreiro ESInsider, University, Developer Group Leader ✭✭

    Hi @itwenty do you find any solution to this? I am stuck on the same problem as you... read in the internet it was a problem of Xcode 11 and tried to downgrade to Xcode 10, but the problem persist, other people talk about adding a custom xcconfig file to the build xcodebuild parameter with a LD_VERIFY_BITCODE=NO value, but I don't know how to pass a xcconfig file to Xamarin iOS Build process... so I am totally stuck.

    Kind regards!

  • JosueYerayJulinFerreiroJosueYerayJulinFerreiro ESInsider, University, Developer Group Leader ✭✭

    Hi @itwenty just was able to fix this issue for my library.... the trick is to not use a static library (.a) create a framework target instead and it works perfectly. Just in case it can help you!

  • itwentyitwenty Member

    Hi @JosueYerayJulinFerreiro I myself haven't been able to find a solution for this. Will try your suggestion of creating a framework instead of static lib. Out of curiosity, did you create a static framework or a dynamic one?

  • JosueYerayJulinFerreiroJosueYerayJulinFerreiro ESInsider, University, Developer Group Leader ✭✭

    @itwenty said:
    Hi @JosueYerayJulinFerreiro I myself haven't been able to find a solution for this. Will try your suggestion of creating a framework instead of static lib. Out of curiosity, did you create a static framework or a dynamic one?

    Hi, I created a dynamic one. One thing that drove me crazy some hours, I added all dependency frameworks both to the dynamic library and the binding library and also added the Xamarin.Swift nuget to the app but the app crashes on start with a dyld _obj_c_class not found... at the end seems like when you create the dynamic library Xcode put the iOS target sdk to the last version available, I just changed it to the same as my xamarin iOS app (iOS 10 in my case) and it worked perfectly.

    Also I added a Build Phase to run a script and generate a universal framework for both simulator and devices:

    if [ "true" == ${ALREADYINVOKED:-false} ]
    then
    echo "RECURSION: Detected, stopping"
    else
    export ALREADYINVOKED="true"
    
    set -e
    BUILD_PATH="${SRCROOT}/build"
    DEPLOYMENT_PATH="${SRCROOT}"
    TARGET_NAME="PhenomiumLib"
    FRAMEWORK_NAME="PhenomiumLib"
    FRAMEWORK="${FRAMEWORK_NAME}.framework"
    FRAMEWORK_PATH="${DEPLOYMENT_PATH}/${FRAMEWORK}"
    
    # clean the build folder
    if [ -d "${BUILD_PATH}" ]; then
        rm -rf "${BUILD_PATH}"
    fi
    
    # build the framework for every architecture using xcodebuild
    #-arch armv7 -arch armv7s
    xcodebuild -target "${TARGET_NAME}" -configuration Release \
        -arch arm64 \
        only_active_arch=no defines_module=yes -sdk "iphoneos"
    
    #-arch i386
    xcodebuild -target "${TARGET_NAME}" -configuration Release \
        -arch x86_64 \
        only_active_arch=no defines_module=yes -sdk "iphonesimulator"
    
    # remove previous version from the deployment path
    if [ -d "${FRAMEWORK_PATH}" ]; then
        rm -rf "${FRAMEWORK_PATH}"
    fi
    
    # copy freshly built version to the deployment path
    cp -r "${BUILD_PATH}/Release-iphoneos/${FRAMEWORK}" "${FRAMEWORK_PATH}"
    
    # merge all the slices and create the fat framework
    lipo -create -output "${FRAMEWORK_PATH}/${FRAMEWORK_NAME}" \
        "${BUILD_PATH}/Release-iphoneos/${FRAMEWORK}/${FRAMEWORK_NAME}" \
        "${BUILD_PATH}/Release-iphonesimulator/${FRAMEWORK}/${FRAMEWORK_NAME}"
    
    # copy Swift module mappings for the simulator
    cp -r "${BUILD_PATH}/Release-iphonesimulator/${FRAMEWORK}/Modules/${FRAMEWORK_NAME}.swiftmodule/" \
        "${FRAMEWORK_PATH}/Modules/${FRAMEWORK_NAME}.swiftmodule"
    
    # clean up the build folder again
    if [ -d "${BUILD_PATH}" ]; then
        rm -rf "${BUILD_PATH}"
    fi
    fi
    

    Hope all this helps!

Sign In or Register to comment.