FFI example from a book cannot find -lanneclib on Windows

Communication error with external c dll with callback example .

I created anneclib.dll and scattered it (and lib), even having tried the full path, but still getting the same error (but with the full path).

Error 1: communication 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-wildebeest \ 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, - the entire archive "" -Wl, -Bstatic "" -Wl, - no-whole-archive "" -Wl, -Bdynamic "" -lanneclib "" - lws2_32 "" -luserenv "" -lcompiler-rt "note: ld: cannot find -lanneclib

Using the Visual Studio Rust Project.

Where can I put it?

 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 } } 
+5
source share
1 answer

In the error message, you will see that the folder [your source folder]\bin\x86_64-pc-windows-gnu added to the library path. You must put your library in this folder. You may also need to add the prefix 'lib' to the library name.

Here is a small example that works for me:

C file with welcome feature:

 #include <stdio.h> void hello() { printf("Hello from C!\n"); } 

Compile the C file into the libhello.c shared library using MinGW:

 gcc -shared -o libhello.dll hello.c 

Rust main.rs file:

 #[link(name = "hello")] extern { fn hello(); } fn main() { unsafe { hello(); } } 

Now you should place (copy) libhello.dll in the subfolder \ bin \ x86_64-pc-windows-gnu:

 + bin + --- x86_64-pc-windows-gnu + --- libhello.dll + main.rs 

And you have to compile it with

 rustc main.rs 

Please note that to run main.exe you also need a copy of libhello.dll next to main.exe or on the system path.

+3
source

Source: https://habr.com/ru/post/988661/


All Articles