How do you attach an icon resource to a Rust application? I've seen how it's done in C but I'm not clear on how it works in Rust. This would be on Windows. I know that Linux and OS X work differently. If anyone has any tips on this for OS X that would be great too.
An easy way to set the icon for your .exe
file is with the winres crate. First, add winres as a build dependency in your Cargo.toml
:
[target.'cfg(windows)'.build-dependencies]
winres = "0.1"
Then, add a build script (a file named build.rs
next to your Cargo.toml
):
use std::io;
#[cfg(windows)] use winres::WindowsResource;
fn main() -> io::Result<()> {
#[cfg(windows)] {
WindowsResource::new()
// This path can be absolute, or relative to your crate root.
.set_icon("assets/icon.ico")
.compile()?;
}
Ok(())
}
Note that this does not update the icon shown in the taskbar or title bar. Setting that must be done via your GUI framework, e.g. iced recently added a way to configure this.
To set the icon on macOS, you need to bundle the executable into an .app
. An .app
is actually a directory, not a file. It looks something like this:
<key>CFBundleExecutable</key>
<string>myapp</string>
<key>CFBundleIconFile</key>
<string>AppIcon.icns</string>
target/release
macOS apps are typically distributed as .dmg
files. A release script could build the binary, bundle it into an .app
, and then bundle that into a .dmg
, along with a symlink to /Applications
to make it easier for the user to “install” the app by moving it there.
Here are sample .dmg
contents, and the corresponding release script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With