Compilation / linking error: Undefined symbols for architecture x86_64

Compilation / linking error: Undefined symbols for architecture x86_64

我的 Rust 编译器版本是:

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

我的 Cargo 版本是:

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

当我遇到这个编译错误时,我正在愉快地编码我的 rust 项目:

    $ 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 错误,但这确实令人惊讶,所以我鼓励您提交它(或者让我知道,我可以做到)。

首先,我创建了一个名为 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 }
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这令人惊讶,因为我们确实在 main 中使用了它。然后编译失败,出现与您看到的相同的链接错误。

解决方法是将 order 模块标记为 public:

pub mod order {