Search code examples
rustrust-cargobevy

I can't run the executable of a game made with Bevy


I was following a bevy tutorial and I thought:

What if I run the executable? After all, I will want to share my games in the future.

When i ran the executable, it said that cannot find 2 dlls, bevy_dylib-5d51f44a630848aa.dll and std-1cd530251ef8500f.dll.
It is interesting because has a file called bevy_dylib.dll in the folder and should searching this.

But I found bevy_dylib-5d51f44a630848aa.dll inside the deps, while std-1cd530251ef8500f.dll seems just don't exist.

Same thing happens with this code:

use bevy::prelude::*;

fn main() {
    App::new().add_system(hello).run();
}

fn hello() {
    println!("Hello");
}

but it doesn't happend when I use cargo run. Other times I've noticed that apparently has some difference between using cargo run and running the executable.

Anyone know how compile correctly a game made with bevy? What I am doing wrong?


Solution

  • First, there is a big difference between development and release. Your development configuration should be focused on low build times, and your release configuration should be focused on portability.

    Bevy recommends using the "dynamic" feature for low build times. If you want to compile for release, though, you should remove that flag. This is most likely where most of your problems come from.

    Another problem that arises is that by default, Rust on Windows depends on the MSVC Runtime. You might want to enable static runtime linking to compile the runtime into the executable. This increases its size a little, but that should be fine.

    The best way to enable static runtime linking on windows is to create the file .cargo/config.toml in your project directory, and then paste into it:

    [target.x86_64-pc-windows-msvc]
    rustflags = ["-Ctarget-feature=+crt-static"]
    

    So to sum up:

    • Remove the "dynamic" flag from bevy
    • Build with the crt-static flag

    Then, you should be able to copy-paste your binary to arbitrary windows systems.

    Note, however, that further dependencies of your project could introduce new runtime dependencies. You might have to deliver the respective .dll files with your executable, then; this is common practice for games.


    Further remarks

    You can check for runtime dependencies with the ldd tool in git-bash. Using it on my executable without +crt-static shows:

    > ldd target/release/<my cratename>.exe
            ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc8c410000)
            KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffc8afc0000)
            KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffc89d80000)
            bcrypt.dll => /c/WINDOWS/System32/bcrypt.dll (0x7ffc89d00000)
            ucrtbase.dll => /c/WINDOWS/System32/ucrtbase.dll (0x7ffc8a370000)
            VCRUNTIME140.dll => /c/WINDOWS/SYSTEM32/VCRUNTIME140.dll (0x7ffc6cba0000)
    

    After linking with +crt-static, it shows:

    > ldd target/release/<my cratename>.exe
            ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc8c410000)
            KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffc8afc0000)
            KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffc89d80000)
            bcrypt.dll => /c/WINDOWS/System32/bcrypt.dll (0x7ffc89d00000)