书中的 FFI 示例在 Windows 下找不到 -lanneclib [英] FFI example from book cannot find -lanneclib under Windows
问题描述
在带有回调示例的外部 c dll 后链接时出错.
我创建了 anneclib.dll 并将其分散(和 lib)甚至尝试了完整路径,但仍然出现相同的错误(但使用完整路径).
I have created anneclib.dll and scattered it ( and the lib) have even tried full path but still get the same error ( but with the full path) .
错误 1 错误:与 gcc
链接失败:退出代码:1 注释:gcc""-Wl,--enable-long-section-names" "-fno-use-linker-plugin""-Wl,--nxcompat" "-static-libgcc" "-m64" "-L" "C:\Program Files\Rust稳定 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib""-o""obj\Debug\Anne.exe" "obj\Debug\Anne.o" "-Wl,--gc-sections""C:\Program Files\Rust 稳定1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libstd-4e7c5e5c.rlib" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libcollections-4e7c5e5c.rlib""C:\Program Files\Rust 稳定1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libunicode-4e7c5e5c.rlib" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\librand-4e7c5e5c.rlib" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\liballoc-4e7c5e5c.rlib" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\liblibc-4e7c5e5c.rlib" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib" "-L" "C:\Program Files\Rust stable1.0\bin\rustlib\x86_64-pc-windows-gnu\lib" "-L" "C:\src\ann\anne.rust\anne.rust\Anne.rust\bin\x86_64-pc-windows-gnu""-L" "C:\src\ann\anne.rust\anne.rust\Anne\bin\x86_64-pc-windows-gnu""-Wl,--whole-archive" "-Wl,-Bstatic" "-Wl,--no-whole-archive""-Wl,-Bdynamic" "-lanneclib" "-lws2_32" "-luserenv" "-lcompiler-rt"注意:ld:找不到 -lanneclib
Error 1 error: linking with
gcc
failed: exit code: 1 note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-static-libgcc" "-m64" "-L" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib" "-o" "obj\Debug\Anne.exe" "obj\Debug\Anne.o" "-Wl,--gc-sections" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libstd-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libcollections-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libunicode-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\librand-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\liballoc-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\liblibc-4e7c5e5c.rlib" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib" "-L" "C:\Program Files\Rust stable 1.0\bin\rustlib\x86_64-pc-windows-gnu\lib" "-L" "C:\src\ann\anne.rust\anne.rust\Anne.rust\bin\x86_64-pc-windows-gnu" "-L" "C:\src\ann\anne.rust\anne.rust\Anne\bin\x86_64-pc-windows-gnu" "-Wl,--whole-archive" "-Wl,-Bstatic" "-Wl,--no-whole-archive" "-Wl,-Bdynamic" "-lanneclib" "-lws2_32" "-luserenv" "-lcompiler-rt" note: ld: cannot find -lanneclib
使用 Visual Studio Rust 项目.
Using the Visual Studio Rust project.
我应该把它放在哪里?
extern fn callback(a: i32) {
println!("I'm called from C with value {0}", a);
}
#[link(name = "anneclib")]
extern {
fn register_callback(cb: extern fn(i32)) -> i32;
fn trigger_callback();
}
fn main() {
unsafe {
register_callback(callback);
trigger_callback(); // Triggers the callback
}
}
推荐答案
在错误信息中可以看到文件夹 [your source folder]\bin\x86_64-pc-windows-gnu
是添加到库路径.您必须将库放入此文件夹中.您可能还需要为库名称添加一个lib"前缀.
In the error message you can see that the folder [your source folder]\bin\x86_64-pc-windows-gnu
is added to the library path. You have to put your library into this folder. You may also have to add a 'lib' prefix to the library name.
这是一个对我有用的小例子:
Here is a small example that works for me:
一个带有 hello 函数的 C 文件:
A C file with a hello-function:
#include <stdio.h>
void hello() {
printf("Hello from C!\n");
}
使用 MinGW 将 C 文件编译为共享库 libhello.c:
Compile the C file to a shared library libhello.c using MinGW:
gcc -shared -o libhello.dll hello.c
Rust 文件 main.rs:
The Rust file main.rs:
#[link(name = "hello")]
extern {
fn hello();
}
fn main() {
unsafe { hello(); }
}
现在您必须将 libhello.dll(的副本)放入子文件夹 \bin\x86_64-pc-windows-gnu:
Now you have to put (a copy of) the libhello.dll into the sub-folder \bin\x86_64-pc-windows-gnu:
+ bin
+ --- x86_64-pc-windows-gnu
+ --- libhello.dll
+ main.rs
你应该能够通过
rustc main.rs
请注意,为了执行 main.exe,您还需要 main.exe 旁边或系统路径中的 libhello.dll 副本.
Note in order to execute the main.exe you also need a copy of the libhello.dll next to the main.exe or in the system path.
这篇关于书中的 FFI 示例在 Windows 下找不到 -lanneclib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!