简体   繁体   English

从 Rust 调用 C 库给出“LINK:致命错误 LNK1181:无法打开输入文件”

[英]Calling C library from Rust gives "LINK : fatal error LNK1181: cannot open input file"

I'm trying to call the STM32 Cube Programmer C libraries from Rust.我正在尝试从 Rust 调用 STM32 Cube Programmer C 库。

The entire code, and branches showing various attempts, are available here: https://github.com/becky112358/rust_c_linking_stm32_cube_programmer完整代码和显示各种尝试的分支可在此处获得: https://github.com/becky112358/rust_c_linking_stm32_cube_programmer

Attempt 1 (in my GitHub repository, branch main )尝试 1(在我的 GitHub 存储库中,分支main

Following the Rust Bindgen tutorial: https://rust-lang.github.io/rust-bindgen/按照 Rust Bindgen 教程: https://rust-lang.github.io/rust-bindgen/

This is my preferred method.这是我的首选方法。 A Rust crate wraps the C library.一个 Rust 箱子包装了 C 库。 Other Rust crates can then include the Rust wrapper crate, and not have to worry about any C libraries.其他 Rust crate 可以包含 Rust wrapper crate,而不必担心任何 C 库。

... in theory. ... 理论上。

The Rust crate wrapping the C library ( libstm32_cube_programmer_sys ) builds ok.包装 C 库 ( libstm32_cube_programmer_sys ) 的 Rust 板条箱构建正常。 Its tests run ok.它的测试运行正常。 The Rust crate calling the Rust crate which wraps the C library ( caller ) does not build, but reports:调用包装 C 库( caller )的 Rust 包装箱的 Rust 包装箱未构建,但报告:

    = note: LINK : fatal error LNK1181: cannot open input file '.\drivers\CubeProgrammer_API.lib'

Why is caller even trying to look for the C library?为什么caller还要尝试查找 C 库? I expected libstm32_cube_programmer_sys to handle all C library to Rust conversion, and that any Rust crates then calling libstm32_cube_programmer_sys could be purely Rusty (with maybe some unsafeness).我希望libstm32_cube_programmer_sys能够处理所有 C 库到 Rust 的转换,并且任何 Rust 板条箱然后调用libstm32_cube_programmer_sys可能是纯粹的 Rusty(可能有些不安全)。

  • In build.rs I initially mis-wrote the C library name, and libstm32_cube_programmer_sys did not build.build.rs我最初错误地写了 C 库名称,并且libstm32_cube_programmer_sys没有构建。 Correcting the library name allowed libstm32_cube_programmer_sys to build successfully.更正库名称允许libstm32_cube_programmer_sys成功构建。 So it seems like libstm32_cube_programmer_sys does open the C library.所以看起来libstm32_cube_programmer_sys确实打开了 C 库。
  • I tried adding the path to the drivers folder to my PATH .我尝试将drivers文件夹的路径添加到我的PATH
  • I tried listing the absolute path to the C library:我尝试列出 C 库的绝对路径:
println!("cargo:rustc-link-lib=C:/[blah blah]/drivers/CubeProgrammer_API");

I could not find how to feed in the path correctly, without Rust reporting:如果没有 Rust 报告,我找不到如何正确输入路径:

    error: renaming of the library `C` was specified, however this crate contains no `#[link(...)]` attributes referencing this library.



Attempt 2 (branch all_in_one )尝试 2(分支all_in_one

In the main branch it seemed like maybe the problem was that libstm32_cube_programmer_sys could find the C library but caller could not.main分支中,问题似乎可能是libstm32_cube_programmer_sys可以找到 C 库,但caller找不到。 So I tried discarding the separate Rust crate, and having a single Rust crate which both wraps the C library and calls the C functions.所以我尝试丢弃单独的 Rust 包装箱,并使用一个 Rust 包装箱,它包装了 C 库并调用了 C 函数。

This time I get the following error, plus a bonus warning:这次我得到以下错误,加上一个额外的警告:

    = note: caller.59pofysds2mkvvjr.rcgu.o : error LNK2019: unresolved external symbol disconnect referenced in function _ZN6caller4main17ha79648c0a9e86ed0E
    .\drivers\CubeProgrammer_API.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'



Attempt 3 (branch link_search )尝试 3(分支link_search

I searched a lot on the inte.net and found lots of different ways to call a C library from Rust. One way is to use link-search rather than link-lib .我在 inte.net 上搜索了很多,发现有很多不同的方法可以从 Rust 调用 C 库。一种方法是使用link-search而不是link-lib This surely only makes things harder for the compiler because you make it do more work.这肯定只会让编译器更难,因为你让它做更多的工作。 But I am stuck and need to try different things!但是我被卡住了,需要尝试不同的东西!

This time I get the following error, plus the bonus warning:这次我得到以下错误,加上奖金警告:

    = note: caller.59pofysds2mkvvjr.rcgu.o : error LNK2019: unresolved external symbol __imp_disconnect referenced in function _ZN6caller4main17ha79648c0a9e86ed0E
    .\drivers\CubeProgrammer_API.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'



Question问题

How do I make this work?我如何使这项工作? Ideally from Attempt 1 , but I'll take anything!最好从Attempt 1开始,但我会接受任何东西!

When we have:当我们有:

C library <- Rust library <- Rust code C 图书馆 <- Rust 图书馆 <- Rust 代码

It seems that看起来

  • when compiling, the Rust code needs to be able to see the C library, even though it is also calling the Rust library编译时,Rust 代码需要能够看到 C 库,即使它也在调用 Rust 库
  • when running, there may be C dlls which you need to store alongside the Rust exe运行时,可能需要将 C dll 与 Rust exe 一起存储

That was my main misunderstanding when I posted my original question.这是我发布原始问题时的主要误解。

Some other tips / reminders:其他一些提示/提醒:

  • Make sure to use the correct lib file, (x64, x86 etc)确保使用正确的 lib 文件(x64、x86 等)
  • Resolve warnings too!也解决警告!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cython:链接:致命错误LNK1181:无法打开输入文件 - Cython: LINK : fatal error LNK1181: cannot open input file 链接…链接:致命错误LNK1181:无法打开输入文件&#39;libgsl.a&#39; - Linking… LINK : fatal error LNK1181: cannot open input file 'libgsl.a' 链接:致命错误LNK1181:无法打开输入文件&#39;libclamav.lib&#39; - LINK : fatal error LNK1181: cannot open input file 'libclamav.lib' 错误 LNK1181:编译为 .LIB 时无法打开输入文件 - Error LNK1181: cannot open input file when compiling as .LIB 在Windows中使用C绑定构建Python包(VC90)会导致LNK1181致命错误 - Build Python package with C bindings in Windows (VC90) causes LNK1181 fatal error 链接:致命错误LNK1104:无法打开文件&#39;C:\\ Users \\ hp \\ .pyxbld \\ lib.win32-2.7 \\ gensim \\ models \\ word2vec_inner.pyd&#39; - LINK : fatal error LNK1104: cannot open file 'C:\Users\hp\.pyxbld\lib.win32-2.7\gensim\models\word2vec_inner.pyd' C MySQL错误“严重错误LNK1107:文件无效或损坏:无法在0x368处读取” - C MySQL error “fatal error LNK1107: invalid or corrupt file: cannot read at 0x368” VisualStudio - 致命错误 LNK1168:无法打开 myfile.exe 进行写入 - VisualStudio - Fatal error LNK1168: cannot open myfile.exe for writing 为什么从 C 调用 Rust 库会导致 memory 泄漏? - Why does calling a Rust library from C result in memory leakage? Microsoft C ++编译器,严重错误C1083:无法打开包含文件“ stdio.h” - Microsoft C++ Compiler, Fatal Error C1083: Cannot open include file 'stdio.h'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM