I wrote a demo for using dll.
this build.rs can compile and link correctly.
fn main(){
let profile =env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
println!("cargo::rustc-link-lib=libr");
println!("cargo::rustc-link-search=target/{profile}/deps");
}
but this build.rs can not compile.
fn main(){
let profile =env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
let _src = format!("target/{profile}/deps/{dll_name}");
println!("cargo::rustc-link-lib=libr");
println!("cargo::rustc-link-search=target/{profile}/deps");
}
I checked build log with "cargo build -vv". The first version found arguments "-L target/debug/deps -l libr", but not in the second version.
I wanna to copy the dll into target/{profile} folders with scripts so the program can run normally. But I'm frustrated with no solution after searching the web and asking AI for a day.
I'm not a English native speaker so forgive my language fault. Chinese reply is okay.
If you're experiencing compilation errors, it can be a good idea to include that error message with your question.
For the code you've shared here, I'm noticing that the variable dll_name
is never introduced. Is that an actual problem or is the version of the build script shown here just incomplete?
1 Like
oh god it may be a bug. Here is another build.rs
fn main(){
let dll_name="libr.dll";
let profile =env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
let src = format!("target/{profile}/deps/{dll_name}");
let dst = format!("target/{profile}/{dll_name}");
fs::copy(Path::new(&src),Path::new(&dst)).expect("需求的dll不存在");
println!("cargo::rustc-link-lib=libr");
println!("cargo::rustc-link-search=target/{profile}/deps");
}
Running with "cargo build --release" works correctly and the script copys the dll to desired location.
How did it fail without "--release" argument?
I don't think the build script re-runs when you switch --release
on or off; which might explain why only one works (probably the one that's the same as when the build script was last run).
I'm not sure off the top of my head what the correct / recommended way of doing DLLs is, but maybe someone else knows more about that.
ok I finally figure out. The project folder's parent folder contains non-ascii characters. After removed "cargo build" works correctly.