rust – 编译/链接错误:体系结构x86_64的未定义符号

我的Rust编译器版本是:

$rustc --version
rustc 1.0.0-nightly (3e4be02b8 2015-03-13) (built 2015-03-13)

我的货物版本是:

$cargo --version
cargo 0.0.1-pre-nightly (66849de 2015-03-10) (built 2015-03-11)

当我遇到这个编译错误时,我很高兴地编写了我的锈项目:

    $cargo build --verbose
       Fresh log v0.2.5
       Fresh rustc-serialize v0.3.6
       Fresh libc v0.1.3
       Fresh rand v0.1.4
       Fresh uuid v0.1.11
   Compiling rexchange v0.0.1 (file:///Users/sam/dev/rexchange)
     Running `rustc src/main.rs --crate-name rexchange --crate-type bin -g --out-dir /Users/sam/dev/rexchange/target/debug --emit=dep-info,link -L dependency=/Users/sam/dev/rexchange/target/debug -L dependency=/Users/sam/dev/rexchange/target/debug/deps --extern uuid=/Users/sam/dev/rexchange/target/debug/deps/libuuid-f80da2be6e37bdc9.rlib --extern rexchange=/Users/sam/dev/rexchange/target/debug/librexchange-436e10ddf2e26a6f.rlib`
error: linking with `cc` failed: exit code: 1
note: "cc" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/sam/dev/rexchange/target/debug/rexchange" "/Users/sam/dev/rexchange/target/debug/rexchange.o" "-Wl,-force_load,/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a" "-Wl,-dead_strip" "-nodefaultlibs" "/Users/sam/dev/rexchange/target/debug/librexchange-436e10ddf2e26a6f.rlib" "/Users/sam/dev/rexchange/target/debug/deps/libuuid-f80da2be6e37bdc9.rlib" "/Users/sam/dev/rexchange/target/debug/deps/librustc-serialize-6bb45c0d7c639d54.rlib" "/Users/sam/dev/rexchange/target/debug/deps/librand-6dfe5258ada5ebf2.rlib" "/Users/sam/dev/rexchange/target/debug/deps/liblibc-2a692f33a70517c8.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcollections-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libunicode-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librand-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e7c5e5c.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-4e7c5e5c.rlib" "-L" "/Users/sam/dev/rexchange/target/debug" "-L" "/Users/sam/dev/rexchange/target/debug/deps" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/sam/dev/rexchange/.rust/lib/x86_64-apple-darwin" "-L" "/Users/sam/dev/rexchange/lib/x86_64-apple-darwin" "-lc" "-lm" "-lSystem" "-lpthread" "-lc" "-lm" "-lcompiler-rt"
note: ld: warning: directory not found for option '-L/Users/sam/dev/rexchange/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/sam/dev/rexchange/lib/x86_64-apple-darwin'
Undefined symbols for architecture x86_64:
  "orders::BidOrder::get_account_id::hbe8193f627eb0daf4ba", referenced from:
      main::h6e33cd0005718795haa in rexchange.o
  "orders::BidOrder::get_currency_price::hac0a15cbc8d29001Gba", referenced from:
      main::h6e33cd0005718795haa in rexchange.o
  "orders::BidOrder::get_commodity_amount::hce97000d2a1749beSba", referenced from:
      main::h6e33cd0005718795haa in rexchange.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error
Could not compile `rexchange`.

Caused by:
  Process didn't exit successfully: `rustc src/main.rs --crate-name rexchange --crate-type bin -g --out-dir /Users/sam/dev/rexchange/target/debug --emit=dep-info,link -L dependency=/Users/sam/dev/rexchange/target/debug -L dependency=/Users/sam/dev/rexchange/target/debug/deps --extern uuid=/Users/sam/dev/rexchange/target/debug/deps/libuuid-f80da2be6e37bdc9.rlib --extern rexchange=/Users/sam/dev/rexchange/target/debug/librexchange-4

我不知道为什么会发生这种失败,我找不到任何类似的东西.任何人都可以向我解释发生了什么以及如何解决?

代码是:https://github.com/samphippen/rexchange

最佳答案 我可以重现这个问题,并有一个解决方法.我不确定它是否是一个Rust bug,但它确实令人惊讶,所以我鼓励你提交它(或者让我知道,我可以做到).

首先,我创建了一个名为repro的新货物项目.然后我添加了这些文件:

lib.rs

mod order {
    pub struct Alpha(pub u8);

    impl Alpha {
        pub fn value(&self) -> u8 { self.0 }
    }
}

pub mod interface {
    use super::order::Alpha;

    pub fn yeah() -> Alpha { Alpha(8) }
}

main.rs

extern crate repro;

fn main() {
    println!("{:?}", repro::interface::yeah().value());
}

在编译期间,会生成此警告:

src/lib.rs:5:9: 5:45 warning: method is never used: `value`, #[warn(dead_code)] on by default
src/lib.rs:5         pub fn value(&self) -> u8 { self.0 }
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这是令人惊讶的,因为我们确实在主要使用它.然后编译失败并显示您看到的相同链接错误.

解决方法是将订单模块标记为公共:

pub mod order {
点赞