rust-objc icon indicating copy to clipboard operation
rust-objc copied to clipboard

How to link struct from framework?

Open KSBilodeau opened this issue 3 years ago • 1 comments
trafficstars

I'm currently attempting to call the controllers functions as seen here in order to get a list of connected controllers on mac. I'm attempting to achieve this with the following code:

use objc::{class, msg_send, sel, sel_impl};
use objc::runtime::{BOOL, Object};

fn main() {
    unsafe {
        let class = class!(GCController);
        let selector = sel!(controllers);
        let obj: *mut Object = msg_send![class, new];

        let _: *mut Object = msg_send![class, selector];

        // Even void methods must have their return type annotated
        let _: () = msg_send![obj, release];
    }
}

However, it fails as follows:

thread 'main' panicked at 'Class with name GCController could not be found', src/main.rs:6:21
stack backtrace:
   0: rust_begin_unwind
             at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/core/src/panicking.rs:142:14
   2: objc_test::main
             at ./src/main.rs:6:21
   3: core::ops::function::FnOnce::call_once
             at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

How do I link against the Controllers framework to get this to work? Also, any advice for calling functions would be incredibly appreciated.

KSBilodeau avatar Jul 10 '22 02:07 KSBilodeau

You can link to the framework using the link attribute:

#[link(name = "GameController", kind = "framework")]
extern "C" {}

Note that it doesn't matter what's inside the extern block.

any advice for calling functions would be incredibly appreciated.

In my fork I've documented the msg_send! macro a bit more, especially with regards to safety requirements, that might help you a bit of the way there. Also beware that you need to follow Cocoa's memory management rules when working with objects.

madsmtm avatar Jul 16 '22 12:07 madsmtm