< 返回版块

LCW-QAQ 发表于 2021-06-15 15:29

Tags:FFI, Link, lib, dll

main.rs

#[link(name = "cdylib_addone", kind = "dylib")]
extern {
    fn add(x: i32, y: i32);
}

fn main() {
    unsafe { add(1, 2); }
}

cdylib_addone.dll Code

#[no_mangle]
pub extern "C" fn add(x: i32, y: i32) {
    println!("{}", x + y);
}

目录结构

  • project

    • src

      • main.rs
    • cdylib_addone.dll

    • Cargo.lock

    • Cargo.toml

错误信息

error: linking with link.exe failed: exit code: 1181

"E:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.1kfxg7tg7t5j8ax8.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.2106qw3ns663remf.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.28td9me9iugo2f32.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.323csw1qkx05jr9r.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.3j4gzpvjakwv9rwo.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.400wq52z9hhpak2p.rcgu.o" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.4kznj7bq3qwiclfj.rcgu.o" "/OUT:D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.exe" "D:\RustLearn\learn01\lib_demo2\target\debug\deps\lib_demo2.4a9y3of0qgfml80a.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\etc\intrinsic.natvis" "/NATVIS:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\etc\liballoc.natvis" "/NATVIS:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\etc\libcore.natvis" "/NATVIS:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\etc\libstd.natvis" "/LIBPATH:D:\RustLearn\learn01\lib_demo2\target\debug\deps" "/LIBPATH:C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "cdylib_addone.lib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libstd-a7e09e5828b15a76.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libpanic_unwind-9aec73fd21b6e5d9.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libstd_detect-cac222343a378d38.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_demangle-03039cad6e730ad8.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libhashbrown-0f9f7f4cab8e582c.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_alloc-0271edfe074f9cf5.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libunwind-1eec1577c7400238.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcfg_if-73a42672b231c1c3.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liblibc-8b49d38d261333b5.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liballoc-492ec249a8bb9c72.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_core-411e087366cd1011.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcore-92e42e605d9acdd0.rlib" "C:\Users\LCW\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcompiler_builtins-58aa2764430eb4d9.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"

= note: Non-UTF-8 output: LINK : fatal error LNK1181: \xce\xde\xb7\xa8\xb4\xf2\xbf\xaa\xca\xe4\xc8\xeb\xce\xc4\xbc\xfe\xa1\xb0cdylib_addone.lib\xa1\xb1\r\n

评论区

写评论
作者 LCW-QAQ 2021-06-15 18:08

感谢pymongo 构建时, 加上build.rs后才只能找到库. 不过加上后还是报错, 我的操作系统是windows, 链接工具从msvc换到mingw64就好了, 应该是msvc的问题, 之前写CPP的时候, 也遇到过, vscode选择msvc编译CPP出了这个错, 没想到还真是msvc的问题.现在我使用的是Clion, 第三方IDE使用msvc可能会出现这个错(原因不明).

ruby 2021-06-15 15:48

你要在build.rs中加上println!("cargo:rustc-link-search=native=.");

意思是让ld的library search path加上当前目录

你可以参考下我以前写的Rust/C互调用动态/静态链接库的笔记:

https://github.com/pymongo/learn_gcc/tree/main/rust_call_c_dll_sll

1 共 2 条评论, 1 页