cross build --target x86_64-pc-windows-gnu --bin trsync_manager_systray --release --features windows
File trsync_manager_systray.exe is generated without error. But, when executed on Windows, give the error :
Unable to create tray item : 'Error setting icon from resource: 1812 at /cargo/registry/src/github.com-1ecc6299db9ec823/tray-item-0.7.0/src/api/windows/funcs.rs#36'
I have two interrogations. First, How cross build --target x86_64-pc-windows-gnu [...] can compile without errors ? Source code need some dependencies (dependencies are they used from my linux and made compatible for windows ?). Second, cross build [...]ignore the build.rs file ? Which explains Error setting icon from resource error ?
Thanks in advance ! I'm a beginner about cross compilations.
It is a mistake to use #[cfg(target_os = …)] in build.rs. This is because #[cfg] refers to the environment in which the build.rs's executable will run, which will be the host (compiler) operating system. It is not the actual compilation target system.
Build scripts have to use Cargo's environmental variables, like CARGO_CFG_TARGET_OS to get actual OS the program is being compiled for.
Just a note: printing cargo:rerun-if-changed=build.rs isn't problematic, so doing it unconditionally is fine and probably better than gating it on "I didn't rerun-if-changed anything else."
use windres::Build;
fn main() {
if std::env::var_os("CARGO_CFG_WINDOWS").is_some() {
Build::new().compile("trsync.rc").unwrap();
println!("cargo:rerun-if-changed=icon.ico");
}
println!("cargo:rerun-if-changed=build.rs");
}
When execute cross build --target x86_64-pc-windows-gnu --bin trsync_manager_systray --release --features windows :
Compiling trsync_manager_systray v0.1.0 (/project/systray)
error[E0432]: unresolved import `windres`
--> systray/build.rs:13:5
|
13 | use windres::Build;
| ^^^^^^^ use of undeclared crate or module `windres`
For more information about this error, try `rustc --explain E0432`.
error: could not compile `trsync_manager_systray` due to previous error
You probably want to use not windres = "*", but windres = "0.2.2", to be sure that the version pulled in is the one you want. windres 0.2.2does have Build in the root, unconditionally.
With explicit "0.2.2" version in Cargo.toml, error is the same. I tried with a fresh project including only Cargo.toml, empty main.rs and the build.rs and same error.
If I take a look on source file/home/myusername/.cargo/registry/src/github.com-1ecc6299db9ec823/windres-0.2.2/src/lib.rs the Build struct is in :
#[derive(Clone, Debug)]
/// A builder for compiling Windows resources.
pub struct Build {
[...]
}
I don't see cfg or target_os condition ... I don't understand.
Looked int o the source myself - I think I see the problem. The whole crate is annotated as #![cfg(target_os = "windows")], therefore when compiling for Unix, it is treated as empty. Seems that cross-compilation is not supported in it, really.