Build exe file for windows

Oh gotcha, it's probably a working directory vs exe directory thing.
So somewhere in your code, you're probably loading resources (maybe fonts / images?). I'm guessing you're specifying the path to it, such as resources/my_image.png.

If you do that, then the application will look for "resources/my_image.png" from the directory you run the application from. I'm guessing your application's resources are in your crate directory which makes it work in development when you run cargo run, but you'd like it to work when you run the application anywhere, and the resources are beside the application.

// in development
my_app
|- resources
|    |- my_image.png  // for example
|
|- src/main.rs
|- target/debug/my_app.exe

// when you publish
anywhere
|- resources
|    |- my_image.png
|
|- my_app.exe

So to make the app find the resource, you probably want to do something like:

// Note: untested

use std::env;
use std::path::PathBuf;

fn load_resources() {
    // See <https://doc.rust-lang.org/stable/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
    let base_dir = option_env!("CARGO_MANIFEST_DIR").map_or_else(|| {
        let exe_path = env::current_exe().expect("Failed to get exe path");
        exe_path.parent.expect("Failed to get exe dir").to_path_buf()
    }, |crate_dir| {
        PathBuf::new(crate_dir);
    });

    let resources_dir = base_dir.join("resources");

    // for each resource
    load(resources_dir.join("my_image.png"));
}

Hope that helps

4 Likes