Making both binary and cdylib from one package

I have a package with two modules hazard and callgraph where hazard uses callgraph, and main.rs uses both. I want to build both a binary (that uses the above modules, but has a bunch of code specific to the binary) and a cdylib for loading into Python. The cydlib doesn't need any of the code in the main.rs file for the binary, but it does need to use both modules. It also has Python-specific code that I don't need linked into the binary, but it won't hurt if it is.

I can't get it to work in a proper way. It's straightforward if the [lib] crate-type = ["rlib"], but if I change it to cdylib, then the binary doesn't seem to link with it. So if I pub use hazard from within lib.rs, main.rs can't access it.

So I thought I'd let them be completely separate and have both main.rs and lib.rs contain mod hazard;. That works. But main.rs needs to use callgraph, and if I pull it in with mod callgraph; then hazard.rs can't get to it.

I can get it to work if I make callgraph be a submodule of hazard. But that's weird -- callgraph is a generic callgraph traversal module, hazard is a module that knows how to load a specific file format to produce a callgraph::Callgraph. At some point, I'll probably publish callgraph to crates.io (is there a customary way to publish things that you want to share so other people can try them out but you know are kinda crap because you're a rust beginner? sfink_callgraph or something?) At that point, I would certainly not want callgraph to be inside hazard.

It would make more sense the other way, I guess, if hazard were a submodule of callgraph. But I still wouldn't really want them to be in the same crate once I publish them, and it seems weird for the module hierarchy to be determined by the desire to produce both a binary and cdylib library. (Not to mention that if they were published, then I think everything would just work out fine with them being toplevel modules. So I would have to change the hierarchy when I published. Bleck.)

I'm hoping that I'm just misunderstanding how all this stuff works.

Have you tried

[lib]
crate-type = ["cdylib", "rlib"]

?

(And no, there isn't a convention for "draft" names on crates.io, but prefixing them with your username while they're experimental would be really polite. I wish crates were namespaced like github, but that ship has sailed.)

1 Like

Doh! Thank you, that works perfectly. It somehow didn't register that crate-type is a list. I was confused because the documentation is very specific about saying that a package can have at most one library.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.