chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
842 stars 62 forks source link

Cannot for the life of me get Xcode + Cargo to work #263

Closed AngeloMateus closed 8 months ago

AngeloMateus commented 8 months ago

After following the steps in Xcode + Cargo everything compiles and runs as expected until I call I a swift function from rust:

mod native/ios.rs:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        #[swift_bridge(swift_name = "main_rs")]
        fn main();
    }
    extern "Swift" {
        #[swift_bridge(swift_name = "setAudioCategory")]
        fn set_audio_category();
    }
}

lib.rs:

pub fn main() {
    native::ios::set_audio_category();
}

myapp.swift

import Foundation
import AVFAudio

@main class MyAppIOS {
    static func main() throws {
        main_rs()
    }
}

func setAudioCategory() {
    do {
        try AVFAudio.AVAudioSession.sharedInstance().setCategory(.ambient);
    } catch {
        print("Couldn't set AVAudioSession category");
    }
}

The strange thing is if I don't call native::ios::set_audio_category(); everything within main runs properly so the extern "Rust" works.

The error I get is:

ld: Undefined symbols:
    ___swift_bridge__$set_audio_category, referenced from:
        ___swift_bridge__$main in myapp.my.8bdd3dadde1ac879-cgu.11.rcgu.o

The generated swift:

public func main_rs() {
    __swift_bridge__$main()
}
@_cdecl("__swift_bridge__$set_audio_category")
func __swift_bridge__set_audio_category () {
    setAudioCategory()
}

I've set the Library search paths to basically everything in target/ at this point I've added both debug and release libraries to Frameworks, Libraries and Embedded Content as well as withing Build Phases. I'm not really sure what I'm missing, is there some way to debug this?

chinedufn commented 8 months ago

My guess would be that the func __swift_bridge__set_audio_category () is getting optimized out and needs to be public func instead.

If you change it to public func does it work?

chinedufn commented 8 months ago

You can subscribe to https://github.com/chinedufn/swift-bridge/pull/262

I think that should fix this.