[英]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
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(可能有些不安全)。
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 库。drivers
folder to my PATH
.我尝试将drivers
文件夹的路径添加到我的PATH
。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.
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'
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'
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看起来
That was my main misunderstanding when I posted my original question.这是我发布原始问题时的主要误解。
Some other tips / reminders:其他一些提示/提醒:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.