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

Linking fails when only a lib's static variables are used and none of its functions are called. #29267

Closed
briansmith opened this issue Oct 24, 2015 · 4 comments

Comments

@briansmith
Copy link
Contributor

Program:

extern crate test_1_lib;

fn main() {
    // Uncomment this function call to make linking succeed:
    // test_1_lib::some_function();

    vec![0u8; test_1_lib::SOME_VALUE.some_member];
}

test_1_lib:

pub struct SomeType {
    pub some_member: usize,
}

pub static SOME_VALUE: SomeType = SomeType {
    some_member: 1,
};

pub fn some_function() { }

The full test case is available at https://github.com/briansmith/rust-bug-2.

To reproduce:

git clone https://github.com/briansmith/rust-bug-2
cd bin
cargo build

Linking fails with:

test_1_bin.0.o : error LNK2019: unresolved external symbol __imp__ZN10SOME_VALUE20hc1593d2370c9bbcfiaaE referenced in fu
nction _ZN4main20hf3c40dca13f06b85faaE
C:\<REDACTED>\test_1_bin.exe : fatal error LNK1120: 1 unresolved externals

Notice that the program only uses a static variable, and no functions, from the
library crate. If you uncomment the function call in
bin/src/test_1_bin.rs then linking succeeds.

Reproduced with (on x86_64-pc-windows-msvc):

  • rustc 1.3.0 (9a92aaf 2015-09-15) / cargo 0.4.0-nightly (553b363 2015-08-03) (built 2015-08-03)
  • rustc 1.4.0-nightly (fd302a9 2015-08-27) / cargo 0.5.0-nightly (53e51d5 2015-08-26)
  • rustc 1.5.0-nightly (7beebbe 2015-10-22) / cargo 0.6.0-nightly (e1ed995 2015-10-22)
@wthrowe
Copy link
Contributor

wthrowe commented Oct 24, 2015

Appears to be Windows-specific. (Compiles successfully with 1.3.0 on 64bit Linux.)

@retep998
Copy link
Member

Duplicate of #27438

@alexcrichton
Copy link
Member

@briansmith when testing on beta/stable, are you sure you used the right compiler? This should have been fixed by #28646 which is present on both the beta/stable compilers, and I have verified myself that the x86_64-pc-windows-msvc beta/stable compilers work where the stable one does not. If it's still broken for you, though, that sounds like a problem!

@briansmith
Copy link
Contributor Author

@briansmith when testing on beta/stable, are you sure you used the right compiler?

I guess you mean "beta/nightly" everywhere you wrote "beta/stable."

I did not test beta. I did test an old 1.4 nightly that failed. But, guess I did not properly test the current 1.5 nightly, because I just tried again and it succeeds. So, I think that this is fixed in Nightly 1.5 based on my testing. Again, I did not test the 1.4 beta.

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

4 participants