Skip to content

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

Closed
@briansmith

Description

@briansmith

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions