mindTree
mindTree

Reputation: 946

How to use a macro from one crate in another?

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?

Upvotes: 11

Views: 4259

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90732

Your attributes are tangled.

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

Here are some things of note:

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

  2. 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 !.

Upvotes: 7

Related Questions