[rust-dev] How to join together C and Rust code?

Brian Anderson banderson at mozilla.com
Mon Apr 16 11:41:17 PDT 2012


On 04/16/2012 06:01 AM, Alexander Stavonin wrote:
> Is it possible to include into the one crate as Rust as C code?

Yes it is possible. There's no explicit support for it but native 
modules may refer to static libraries and it's relatively easy to link 
arbitrary static libraries into a crate. rust-azure[1] does this so it 
doesn't have to worry about keeping track of a native dynamic library.

It's not quite ergonomic right now but rust-azure does it by including 
the following hack (in test.rs of all places):

     #[link_args = "-L. -lcairo -lazure"]
     #[nolink]
     native mod m { }

The native mod is just a place to tack on the extra link args, `-L` adds 
to the library search path, `-lcairo` is a system-installed dependency 
of azure, and `-lazure` is the static library that is linked into the 
crate. There are a few[2] upcoming changes[3] that will make
this eaisier in the future.

-Brian

[1]:https://github.com/brson/rust-azure
[2]:https://github.com/mozilla/rust/issues/2217
[3]:https://github.com/mozilla/rust/issues/2218


More information about the Rust-dev mailing list