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

Can't find struct from other crate #50

Closed
dbrgn opened this issue Sep 3, 2017 · 4 comments
Closed

Can't find struct from other crate #50

dbrgn opened this issue Sep 3, 2017 · 4 comments

Comments

@dbrgn
Copy link
Contributor

dbrgn commented Sep 3, 2017

Steps to reproduce:

$ git clone https://github.com/dbrgn/svg2polylines
$ cd svg2polylines
$ cat >cbindgen.toml <<EOL
language = "C"

[parse]
parse_deps = true
include = ["svg2polylines"]
$ cbindgen -v svg2polylines-ffi/src/lib.rs -c cbindgen.toml -o svg2polylines.h
INFO: take ::Polyline
INFO: take ::svg_str_to_polylines
INFO: take ::free_polylines
WARN: can't find CoordinatePair
WARN: can't find size_t
WARN: can't find size_t
WARN: can't find size_t

The CoordinatePair struct is defined in the svg2polylines crate and is pretty simple:

/// A CoordinatePair consists of an x and y coordinate.    
#[derive(Debug, PartialEq, Copy, Clone)]    
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]    
#[repr(C)]    
pub struct CoordinatePair {                                                         
    pub x: f64,    
    pub y: f64,    
}    

What's the reason why it can't be found?

@dbrgn dbrgn changed the title Can't find struct from third party crate Can't find struct from other crate Sep 3, 2017
@Michael-F-Bryan
Copy link
Contributor

What's the reason why it can't be found?

My understanding is that cbindgen only deals with source code and the AST. So it'll parse your crate and figure out what files it imports, but because it doesn't use the compiler under the hood it can't resolve any imports from outside your crate.

I'm not sure there's an easy fix for this short of hooking into compiler internals (I actually started writing a guide for doing this), which would constrain cbindgen to nightly because compiler internals are always going to be unstable.

@eqrion
Copy link
Collaborator

eqrion commented Sep 27, 2017

What's the reason why it can't be found?

The issue here is that you are specifying a specific file, instead of a crate. This causes cbindgen to use single source mode which doesn't parse external crates (because it doesn't have context to know where external crates are). A warning here would probably be a good idea.

My understanding is that cbindgen only deals with source code and the AST. So it'll parse your crate and figure out what files it imports, but because it doesn't use the compiler under the hood it can't resolve any imports from outside your crate.

This is partially true. cbindgen can be configured to emulate cargo and rustc's crate searching behavior, without using compiler internals. This only works with crate parsing mode, which is the bug here.

@eqrion
Copy link
Collaborator

eqrion commented Sep 27, 2017

Forgot to add that the command should probably be:

$ cbindgen -v svg2polylines-ffi/ -c cbindgen.toml -o svg2polylines.h

@eqrion
Copy link
Collaborator

eqrion commented Nov 14, 2017

I've added a warning for this in version 0.2.1. Closing this for now as there is a solution.

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

3 participants