0

I am trying to learn by doing a project of allowing AppKit certain functions , related with App and Window creation to be called from Rust using objc.

extern crate objc;
use objc::runtime::{Class,Object};
use objc::{msg_send, sel, sel_impl};

pub fn create_app(){
    unsafe {
        let app_class =Class::get("NSApplication").unwrap();
        let app:&Object = msg_send![app_class,sharedApplication];
        let _:&Object = msg_send![app_class,run];
    }
}

However if i try to run this an main.rs I get the following:

thread 'main' panicked at src/lib.rs:7:52:
called `Option::unwrap()` on a `None` value

I guess the objective-c is unable to find the NSApplication Class of AppKit but why and how can i resolve such an issue?

I am running all of this from a M3 MacOs just to give insight on my machine.

7
  • Note that extern crate is redundant in modern versions of Rust. Commented Jul 6 at 21:28
  • Did not know that , thanks!
    – user655941
    Commented Jul 6 at 21:30
  • Also note that objc appears abandoned, last commit 4 years ago. Maybe the author just considers it complete, but consider switching to a different crate, probably objc2. Commented Jul 6 at 21:31
  • If you use the class!() macro, does it work? Commented Jul 6 at 21:34
  • Class with name NsApplication could not be found. Will try the objc2 maybe this one is just abandonded as you suggested.
    – user655941
    Commented Jul 6 at 21:38

1 Answer 1

1

I dont have the full knowledge to explain as to why the below solution was correct , if anyone could explain it better you are more then welcome. Seems like running it using the following command helps it succesfully find the AppKit classes using the objc2 crate.

RUSTFLAGS="-C link-arg=-lobjc -C link-arg=-framework -C link-arg=AppKit" cargo run --release
2
  • The Objective C runtime was not linked, which leaded to failure to find the class. It is weird, even a bug, that objc didn't forced linking it. Commented Jul 7 at 17:53
  • You can do that in a build script too. Commented Jul 8 at 14:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.