2

我刚刚意识到,当我使用regex_macroscrate 时,通过 Cargo 创建的 Rust 二进制文件是动态链接的。这有点问题,因为当我更新到更新版本的regex_macros.

有办法解决吗?

这是一个最小的例子:

#![feature(plugin)]
#[plugin]
extern crate regex_macros;


fn main() {}

编译它并查看链接的内容揭示了这一点:

uh@macaron:~/linking-test (master)$ otool -L target/linking-test
target/linking-test:
    /Users/uh/linking-test/target/deps/libregex_macros-bdbdbfedad0748ac.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/librustc-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libflate-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libgetopts-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/librustc_back-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libsyntax-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libfmt_macros-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libarena-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libterm-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libgraphviz-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/librbml-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libserialize-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/liblog-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/librustc_llvm-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)

如您所见,第一个链接的 dylib 有一个进入项目所在目录的绝对路径,即使这样做cargo clean也可能会破坏可执行文件。

4

2 回答 2

3

BurntSushi5#[no_link]的评论中提到了这里缺少的微小细节。因此将代码更改为:

#![feature(plugin)]
#[plugin] #[no_link] extern crate regex_macros;


fn main() {}

将静态链接 regex_macros crate。

于 2015-02-04T13:25:30.070 回答
2

我认为您误认为二进制文件是动态链接的(至少默认情况下不是)。

我刚刚使用 Cargo ( ) 创建了一个全新的二进制项目cargo new --bin foo,添加了正则表达式箱并在源代码中使用它。我编译了二进制文件,然后我用来otool列出动态库(我在 OS X 上):

$ otool -L target/statik
target/statik:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

这似乎表明唯一的动态链接是核心系统库(在 Linux 上,我相信它是 glibc)。

于 2015-02-03T16:23:15.990 回答