gpt4 book ai didi

rust - 静态链接使用 regex_macros 的二进制文件

转载 作者:行者123 更新时间:2023-11-29 08:04:15 24 4
gpt4 key购买 nike

我刚刚意识到,当我使用 regex_macros crate 时,通过 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 也可能会破坏可执行文件。

最佳答案

这里缺少的微小细节是 #[no_link],正如 BurntSushi5 的评论中提到的那样.所以将代码更改为:

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


fn main() {}

将静态链接 regex_macros 包。

关于rust - 静态链接使用 regex_macros 的二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28294105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com