我刚刚意识到当我使用regex_macros
包时,通过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
也可能会破坏可执行文件。
答案 0 :(得分:3)
#[no_link]
在BurntSushi5的评论中提及#![feature(plugin)]
#[plugin] #[no_link] extern crate regex_macros;
fn main() {}
。所以将代码更改为:
{{1}}
将静态链接regex_macros包。
答案 1 :(得分:2)
我认为你错误地认为二进制文件是动态链接的(至少在默认情况下不是这样)。
我刚刚使用Cargo(cargo new --bin foo
)创建了一个全新的二元项目,添加了regex crate并在源代码中使用了它。我编译了二进制文件,然后使用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)。