Glitches from macros from external crate


#1

How to import macro from external crate? Crate is imported in Cargo.toml, used #[macro_export] before macro, used #[macro_use(macro_name)] before importing crate in code. But i am always getting this error: (i hate 4 spaces, because it’s too hard to use them to highlight code. So, no code block. Just plain text)

ancient@lnotebook:~/helium/helium_test$ cargo clean && cargo build
Compiling helium v0.1.0 (file:///home/ancient/helium/helium#6e231afe)
Compiling helium_test v0.1.0 (file:///home/ancient/helium/helium_test)
src/main.rs:2:13: 2:18 error: imported macro not found [E0469]
src/main.rs:2 #[macro_use(he_mk)]
^~~~~
error: aborting due to previous error
Could not compile helium_test.

To learn more, run the command again with --verbose.


#2

Surround it with backticks/backquotes instead.

```text
Your text here
```

should give you a plain text block. It’s still readable, so no worries, but now you know, :slight_smile:

As for your problem, it’s hard to say exactly what’s going on without a code sample, but just to be sure, are the macros defined in a module? If that’s the case, then you will have to add #[macro_use] to that module as well:

In lib.rs:

#[macro_use]
pub mod macros;

in macros.rs:

#[macro_export]
macro_rules! he_mk {
    //...
}

#3

The #[macro_use] thing is not needed for exporting macros out of a crate. Only for having macros in a module used in the rest of the same crate.


#4

Really? Then I don’t know what may be the reason. :smile:


#5

This is located in lib.rs from my library’s crate:

#[macro_export]
macro_rules! he_mk {
    //...
}

This is located in main.rs from crate, in which tests are located:

#[macro_use(he_mk)]
extern crate helium;

#6

It looks right.


#7

Source codes of test:

#[macro_use(he_mk)]
extern crate helium;

fn main(){
    helium::he_mk!(init, html);
    helium::he_mk!(head, html, title => "HI!");
    helium::he_mk!(body, html);
        helium::he_mk!(p, html, text => "Hello, world");
    helium::he_mk!(end, html);
    println!("{}", html);
}

Shell:

ancient@NickolayPC:~/code/tests/helium_test/src$ cargo clean && cargo build
   Compiling helium v0.1.0 (file:///home/ancient/git/helium/#6e231afe)
   Compiling helium_test v0.1.0 (file:///home/ancient/code/tests/helium_test)
main.rs:1:13: 1:18 error: imported macro not found [E0469]
main.rs:1 #[macro_use(he_mk)]
                      ^~~~~
error: aborting due to previous error
Could not compile `helium_test`.

To learn more, run the command again with --verbose.

#8

Source codes of library (not finished): https://bitbucket.org/just_any_team/helium/


#9

This is not your current problem, but macros do not currently use the module namespace system, and :: in macro names means nothing. Once you get import working, it’ll just be he_mk!, no qualifier.

Stupid question, but that version of lib.rs lacks #[macro_export], which would seem to be why it’s not exporting the macro. Is it possible you’re having Cargo.lock problems?


#10
  1. I tried without helium:: prefix too
  2. I forgot to push, sorry
ancient@NickolayPC:~/code/tests/helium_test$ cat src/main.rs 
#[macro_use(he_mk)]
extern crate helium;

fn main(){
    he_mk!(init, html);
    he_mk!(head, html, title => "HI!");
    he_mk!(body, html);
        he_mk!(p, html, text => "Hello, world");
    he_mk!(end, html);
    println!("{}", html);
}
ancient@NickolayPC:~/code/tests/helium_test$ cargo clean && cargo build
   Compiling helium v0.1.0 (file:///home/ancient/git/helium/#6e231afe)
   Compiling helium_test v0.1.0 (file:///home/ancient/code/tests/helium_test)
src/main.rs:1:13: 1:18 error: imported macro not found [E0469]
src/main.rs:1 #[macro_use(he_mk)]
                          ^~~~~
error: aborting due to previous error
Could not compile `helium_test`.

To learn more, run the command again with --verbose.

#11

Ohhh. I just started wrong command and not deleted Cargo.lock