Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to link struct from framework? #115

Open
KSBilodeau opened this issue Jul 10, 2022 · 1 comment
Open

How to link struct from framework? #115

KSBilodeau opened this issue Jul 10, 2022 · 1 comment

Comments

@KSBilodeau
Copy link

KSBilodeau commented Jul 10, 2022

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.

@madsmtm
Copy link

madsmtm commented Jul 16, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants