How to use a macro from one crate in another?

macros, rust, rust-crates

Solution

Your attributes are tangled.

`#![…]` refers to the outer scope, while `#[…]` refers to the next item.

Here are some things of note:

In `lib.rs`, the `#![feature(phase)]` is unnecessary, and the `#![phase(syntax)]` is meaningless.

In `other_project.rs`, your `phase` attribute is applied to the crate, not to the `extern crate dsp;` item—this is why it does not load any macros from it. Remove the `!`.

Problem

I'm struggling to make macros from my rust lib available to other rust projects. Here's an example of how I'm trying to get this work at the moment. `lib.rs`: ``` #![crate_name = "dsp"] #![feature(macro_rules, phase)] #![phase(syntax)] pub mod macros; ``` `macros.rs`: ``` #![macro_escape] #[macro_export] macro_rules! macro(...) ``` `other_project.rs`: ``` #![feature(phase, macro_rules)] #![phase(syntax, plugin, link)] extern crate dsp; macro!(...) // error: macro undefined: 'macro!' ``` Am I on the right track? I've been trying to use std::macros as a reference, but I don't seem to be having much luck. Is there anything obvious that I'm missing?

Original source