Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linker error when returning (and using) a struct that is not exported #31464

Closed
mttnff opened this issue Feb 7, 2016 · 3 comments
Closed

linker error when returning (and using) a struct that is not exported #31464

mttnff opened this issue Feb 7, 2016 · 3 comments

Comments

@mttnff
Copy link

mttnff commented Feb 7, 2016

I just came across a linker error, which should probably give an error during compilation instead. Minimal example:

lib.rs:

mod stuff {
    pub struct MyStruct(u8);

    //some very useful struct
    impl MyStruct {
        pub fn new() -> MyStruct { MyStruct(42) }
        pub fn get_data(self) -> u8 { self.0 }
    }
}

use stuff::MyStruct;

pub fn foo() -> MyStruct {
    MyStruct::new()
}

When adding a binary target bin/exe.rs like this:

extern crate crash;

fn main() {
    //linker error here
    let n = crash::foo().get_data();
    println!("Got: {}",n);
}

I get the following linker error on cargo build or cargo build --bin exe:

note: /.../crash/target/debug/exe.0.o: In function `exe::main':
/.../crash/src/bin/exe.rs:5: undefined reference to `stuff::_$LT$impl$GT$::get_data::hda4b62b5e3688f36taa'
collect2: error: ld returned 1 exit status

I am not familiar with the way rust handles modules, but I think this should indeed not work, so the compiler should probably give an error here.

Note that this compiles (and runs) fine if pub mod stuff is used instead of mod stuff, or if pub use stuff::MyStruct is used, which would make MyStruct accessible.

Compiler versions used:

rustc 1.6.0 (c30b771ad 2016-01-19)
rustc 1.8.0-nightly (3623797eb 2016-02-07)
cargo 0.7.0-nightly (1af03be 2015-12-08)
@sfackler
Copy link
Member

sfackler commented Feb 7, 2016

This is definitely a bug, but you should be able to get things to work by using pub use stuff::MyStruct instead of use stuff::MyStruct.

@petrochenkov
Copy link
Contributor

Duplicate of #16734

@mttnff
Copy link
Author

mttnff commented Feb 7, 2016

Indeed, sorry for missing out on that.

@mttnff mttnff closed this as completed Feb 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants