Rust 在发布版中包含 DLL

huangapple go评论67阅读模式
英文:

Rust include DLL in release

问题

我正在Windows上尝试构建一个使用Rust的torch crate的项目的发布版本。
要安装它,我只需在Cargo.toml中添加如下内容:

[dependencies]
tch = "0.4.0"

如果我运行cargo run,一切都正常。但是,如果我执行生成的.exe文件,我会收到以下错误信息:

torch_cu.dll未找到
c10.dll未找到

我尝试下载了一个已编译的libtorch版本,并将其/lib/中的所有内容包含到相同的文件夹中,现在出现错误:

找不到入口点

我尝试通过向我的项目添加.cargo/config.toml文件,并添加以下内容来进行静态链接:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

正如这个帖子建议的,但没有任何变化。

我无法理解tch的readme对此的解释。

如何处理这类问题?感谢任何帮助!我对这种链接错误的处理方法还很新手...

我尝试这些解决方案的沙箱项目链接在这里:https://github.com/titoco3000/torch_test

英文:

Im in windows, trying to build a release of a project that uses rust's torch crate.
To install it, I simply added to Cargo.toml:

[dependencies]
tch = "0.4.0"

If I do cargo run, it goes ok. But if I execute the produced .exe, I get these errors:

torch_cu.dll not found
c10.dll not found

I tried downloading a compiled version of libtorch and including everything from its /lib/ to the same folder, now with the error:

procedure entry point not found

I tried statically linking everything by adding a .cargo/config.toml to my project, with the contents:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

as suggested by this post, but nothing changed.

I wasnt able to understand what the tch's readme said about it either.

How do I work with these sort of issues?
Thanks for any help! Im new to working on these sort of linking errors...

The sandbox-project I was trying these solutions: https://github.com/titoco3000/torch_test

答案1

得分: 1

对于遇到与我相同问题的人...我认为tch-rs crate使用的libtorch版本可能不是最新的,因为仅仅将libtorch/lib文件夹中的所有dll文件复制粘贴到相同的文件夹中不起作用;但是将rust放置在\target\debug\build\torch-sys-cba3ef2adb59743a\out\libtorch\libtorch\lib\路径下的所有dll文件复制到\target\debug\路径下就可以了!这似乎是一个可怕的解决方案,但至少有效。

英文:

For anyone with the same issue as I was... I think the version of libtorch that the tch-rs crate uses is not the latest, because just copy-pasting all dlls from libtorch/lib to the same folder didnt work; but copying all dlls that were placed by rust at

\target\debug\build\torch-sys-cba3ef2adb59743a\out\libtorch\libtorch\lib\

to

\target\debug\

does the work! It seens like an terrible solution, but at least works.

答案2

得分: 0

如文档中所提到的,您可以在编译之前导出LIBTORCH_STATIC=1环境变量来静态链接pytorch。完整文档在此

至于target-feature=+crt-static,它仅影响C运行时(crt是它的缩写),因此它只会将Visual C++运行时链接到您的应用程序中,不会影响其他DLL。由于PyTorch不是C运行时的一部分,它不会以任何方式受到影响。

英文:

As mentioned in the docs, you can link pytorch statically by exporting LIBTORCH_STATIC=1 environment variable before compilation. Full docs here.

As for target-feature=+crt-static, it affects only C Runtime (crt is abbreviation from it) so it would just link Visual C++ Runtime into your application and wouldn't affect other DLLs. Since PyTorch is not a part of C Runtime, it wouldn't affect it any way.

huangapple
  • 本文由 发表于 2023年6月16日 03:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定