mozilla / mozilla/cbindgen

How to generate headers for third party crate?

Open
#1,038 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
386
Avg merge
1h 43m
Merged PRs (30d)
1

Description

Here is a rust example to illustrate the problem

my current crate

use crate::canvas::Canvas;
// app_surface is third party crate
pub use app_surface::{AppSurface, IOSViewObj};

#[no_mangle]
pub extern "C" fn create_canvas(ios_obj: IOSViewObj) -> *mut libc::c_void {
    println!(
        "create_canvas, maximum frames: {}",
        ios_obj.maximum_frames
    );
    
    let obj = Canvas::new(AppSurface::new(ios_obj), 0_i32);
    
    let box_obj = Box::new(obj);
    // 将 box_obj 对象的内存管理权转交给调用方
    Box::into_raw(box_obj) as *mut libc::c_void
}

third party crate


#[repr(C)]
pub struct IOSViewObj {
    pub view: *mut Object,
    pub metal_layer: *mut c_void,
    pub maximum_frames: i32,
    pub callback_to_swift: extern "C" fn(arg: i32),
}

No header found for IOSViewObj, got warning:

WARN: Can't find IOSViewObj. This usually means that this type was incompatible or not found.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the warning with the two Rust snippets and inspect how cbindgen resolves the pub use of IOSViewObj from app_surface. Check whether third-party #[repr(C)] types containing an extern "C" callback are supported, and identify the configuration or behavior needed for the header to include IOSViewObj.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.