gtk-rs / gtk3-rs

Rust bindings for GTK 3
https://gtk-rs.org
MIT License
507 stars 90 forks source link

[Help] (cargo build -vv) run across (exit code: 0xc0000139, STATUS_ENTRYPOINT_NOT_FOUND) #393

Closed stuartZhang closed 3 years ago

stuartZhang commented 3 years ago

Hello,

  1. My dev box: Windows 10 x64
  2. The rustup toolchain is (nightly-x86_64-pc-windows-gnu)

According to the tutorial, I have done the following steps:

  1. Install msys2
  2. install packages in msys2:
    1. base-devel
    2. mingw-w64-x86_64-toolchain
    3. mingw-w64-x86_64-cmake
    4. mingw-w64-x86_64-make
    5. mingw-w64-x86_64-gcc
    6. curl
    7. tar
    8. git
    9. mingw-w64-x86_64-gtk3
    10. mingw-w64-x86_64-gtk4
  3. The msys2 env variable (PATH):
    1. /mingw64/bin:
    2. /c/Users/StuartZhang/.cargo/bin:
    3. /usr/local/bin:
    4. /usr/bin:/bin:
    5. /opt/bin:
    6. /c/Windows/System32:
    7. /c/Windows:
    8. /c/Windows/System32/Wbem:
    9. /c/Windows/System32/WindowsPowerShell/v1.0/:
    10. /usr/bin/site_perl:
    11. /usr/bin/vendor_perl:
    12. /usr/bin/core_perl

image

  1. git clone the repository
  2. Go to the examples folder
  3. run (cargo build -vv)
  4. run across the failure feedback

Compiling gtk-rs-examples v0.0.1 (H:\github_projects\gtk-rs\examples) Running set CARGO='\\?\H:\symbol_link_source\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin\cargo.exe'&& set CARGO_CRATE_NAME=build_script_build&& set CARGO_MANIFEST_DIR='H:\github_projects\gtk-rs\examples'&& set CARGO_PKG_AUTHORS='The Gtk-rs Project Developers'&& set CARGO_PKG_DESCRIPTION=''&& set CARGO_PKG_HOMEPAGE=''&& set CARGO_PKG_LICENSE=''&& set CARGO_PKG_LICENSE_FILE=''&& set CARGO_PKG_NAME=gtk-rs-examples&& set CARGO_PKG_REPOSITORY=''&& set CARGO_PKG_VERSION=0.0.1&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=0&& set CARGO_PKG_VERSION_PATCH=1&& set CARGO_PKG_VERSION_PRE=''&& set CARGO_PRIMARY_PACKAGE=1&& set PATH='H:\github_projects\gtk-rs\target\debug\deps;H:\symbol_link_source\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin;C:/Users/StuartZhang\.cargo\bin;C:/Users/StuartZhang\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin;C:\msys64\mingw64\bin;C:\Users\StuartZhang\.cargo\bin'&& rustc --crate-name build_script_build --edition=2018 'examples\build.rs' --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=ef7eb270685706d0 -C extra-filename=-ef7eb270685706d0 --out-dir 'H:\github_projects\gtk-rs\target\debug\build\gtk-rs-examples-ef7eb270685706d0' -C 'incremental=H:\github_projects\gtk-rs\target\debug\incremental' -L 'dependency=H:\github_projects\gtk-rs\target\debug\deps' --extern 'gio=H:\github_projects\gtk-rs\target\debug\deps\libgio-0c73d35d695b68b3.rlib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/Users/StuartZhang\.cargo\registry\src\github.com-1ecc6299db9ec823\winapi-x86_64-pc-windows-gnu-0.4.0\lib' Running set CARGO='\\?\H:\symbol_link_source\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin\cargo.exe'&& set CARGO_CRATE_NAME=gtk&& set CARGO_MANIFEST_DIR='H:\github_projects\gtk-rs\gtk'&& set CARGO_PKG_AUTHORS='The Gtk-rs Project Developers'&& set CARGO_PKG_DESCRIPTION='Rust bindings for the GTK+ 3 library'&& set CARGO_PKG_HOMEPAGE='https://gtk-rs.org/'&& set CARGO_PKG_LICENSE=MIT&& set CARGO_PKG_LICENSE_FILE=''&& set CARGO_PKG_NAME=gtk&& set CARGO_PKG_REPOSITORY='https://github.com/gtk-rs/gtk-rs'&& set CARGO_PKG_VERSION=0.13.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=13&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=''&& set OUT_DIR='H:\github_projects\gtk-rs\target\debug\build\gtk-94fd3ddcc83b984a\out'&& set PATH='H:\github_projects\gtk-rs\target\debug\deps;H:\symbol_link_source\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin;C:/Users/StuartZhang\.cargo\bin;C:/Users/StuartZhang\.rustup\toolchains\nightly-x86_64-pc-windows-gnu\bin;C:\msys64\mingw64\bin;C:\Users\StuartZhang\.cargo\bin'&& rustc --crate-name gtk --edition=2018 'gtk\src\lib.rs' --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=6d7ddc49f0772cff -C extra-filename=-6d7ddc49f0772cff --out-dir 'H:\github_projects\gtk-rs\target\debug\deps' -C 'incremental=H:\github_projects\gtk-rs\target\debug\incremental' -L 'dependency=H:\github_projects\gtk-rs\target\debug\deps' --extern 'atk=H:\github_projects\gtk-rs\target\debug\deps\libatk-02414e2791976a8b.rmeta' --extern 'bitflags=H:\github_projects\gtk-rs\target\debug\deps\libbitflags-d894554aa4538f13.rmeta' --extern 'cairo=H:\github_projects\gtk-rs\target\debug\deps\libcairo-d0b4abcaf5f10220.rmeta' --extern 'field_offset=H:\github_projects\gtk-rs\target\debug\deps\libfield_offset-18e8c181390102e4.rmeta' --extern 'futures_channel=H:\github_projects\gtk-rs\target\debug\deps\libfutures_channel-8c715031cceb0909.rmeta' --extern 'gdk=H:\github_projects\gtk-rs\target\debug\deps\libgdk-1b17ac7c033f100f.rmeta' --extern 'gdk_pixbuf=H:\github_projects\gtk-rs\target\debug\deps\libgdk_pixbuf-996818c25ab2d816.rmeta' --extern 'gio=H:\github_projects\gtk-rs\target\debug\deps\libgio-0c73d35d695b68b3.rmeta' --extern 'glib=H:\github_projects\gtk-rs\target\debug\deps\libglib-be27d5b39ace3ed0.rmeta' --extern 'ffi=H:\github_projects\gtk-rs\target\debug\deps\libgtk_sys-82dc556ccef03b67.rmeta' --extern 'gtk3_macros=H:\github_projects\gtk-rs\target\debug\deps\gtk3_macros-82bf6bbb9342f0f9.dll' --extern 'libc=H:\github_projects\gtk-rs\target\debug\deps\liblibc-d43da9ced95ab02c.rmeta' --extern 'once_cell=H:\github_projects\gtk-rs\target\debug\deps\libonce_cell-1f004813157b08a2.rmeta' --extern 'pango=H:\github_projects\gtk-rs\target\debug\deps\libpango-f6c03630da4deb23.rmeta' --cfg 'gdk_backend="broadway"' --cfg 'gdk_backend="win32"' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/Users/StuartZhang\.cargo\registry\src\github.com-1ecc6299db9ec823\winapi-x86_64-pc-windows-gnu-0.4.0\lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' -L 'native=C:/msys64/mingw64/lib' Running H:\github_projects\gtk-rs\target\debug\build\gtk-rs-examples-ef7eb270685706d0\build-script-build error: failed to run custom build command for gtk-rs-examples v0.0.1 (H:\github_projects\gtk-rs\examples)

Caused by: process didn't exit successfully: H:\github_projects\gtk-rs\target\debug\build\gtk-rs-examples-ef7eb270685706d0\build-script-build (exit code: 0xc0000139, STATUS_ENTRYPOINT_NOT_FOUND) warning: build failed, waiting for other jobs to finish... error: build failed

My question: which action I have taken is wrong?

sdroege commented 3 years ago

That means that the examples build.rs failed to run. Not much opportunity for that one to fail really.

I assume you don't have libglib-2.0-0.dll and libgio-2.0-0.dll (or without the lib-prefix) not in your PATH?

Try running https://github.com/lucasg/Dependencies on H:\github_projects\gtk-rs\target\debug\build\gtk-rs-examples-ef7eb270685706d0\build-script-build to see which DLLs it doesn't find.

stuartZhang commented 3 years ago

Hello @sdroege ,

Indeed, there are the (libglib-2.0-0.dll) and (libgio-2.0-0.dll) in (msys2) directory:

image

image

Maybe, is it helpful for me to uninstall them from (msys2)?

sdroege commented 3 years ago

You mean you have multiple versions in your PATH? That seems problematic, yes.

stuartZhang commented 3 years ago

You mean you have multiple versions in your PATH? That seems problematic, yes.

@sdroege I discovered that both (libglib-2.0-0.dll) and (libgio-2.0-0.dll) are just one of (mingw-w64-x86_64-gtk3)'s dependencies. It means there aren't multiple versions. In fact,

mingw-w64-x86_64-gtk3 --> mingw-w64-x86_64-glib2 --> (libglib-2.0-0.dll) and (libgio-2.0-0.dll)

image

stuartZhang commented 3 years ago

@sdroege I run (DependenciesGui.exe) from (msys2) so that all ENV variables visible to (msys2) are available to (DependenciesGui.exe).

image

image

I lose the DLL files:

image

sdroege commented 3 years ago

That seems like a problem in your setup then, best to bring that up with the msys64 project where you got the binaries from.

stuartZhang commented 3 years ago

Hello @sdroege , I have just resolved the problem utterly. The root reason is

  1. neither the erroneous PATH environment.
  2. nor Multiple versions of the zlib package.

On the contrary, (libgio-2.0-0.dll) enables (SafeDllSearchMode). That's to say,

image

the referenced article

As a result, regardless of what the PATH env variable is, the DLL files under the (C:/Windows/System32) folder always take precedence over others. The merely workaround is to place (zlib1.dll) in the same folder as the (*.exe) file that (cargo run) outputs.

Conclusion:

  1. My procedure for setup is correct.
  2. Due to (SafeDllSearchMode) of (libgio-2.0-0.dll), the build artifact doesn't run and complains STATUS_ENTRYPOINT_NOT_FOUND.
  3. Copy the (msys2)'s (zlib1.dll) from C:/msys2/mingw64/bin to the (target/debug) folder under the RUST-bin project.
  4. The problem is fixed.
  5. Finally, the failure merely happens in Windows OS.
stuartZhang commented 3 years ago

@sdroege Do you know how to disable (SafeDllSearchMode) partly and temporarily?

sdroege commented 3 years ago

Sorry, I don't

nirbheek commented 3 years ago

There are more details about safe dll search here: https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

There is no way to make windows load DLLs from PATH first. You should basically never put third-party DLLs in C:\Windows\System32. Any installer that does that is broken. The DLL search order exists so that system libraries are always loaded correctly.

stuartZhang commented 3 years ago

Hello @nirbheek @sdroege ,

I submit my awesome solution for the hassle specific to Windows OS:

  1. Under the project-root directory, write a new file named "build.rs" and including the following codes. Here, I have to paste the source code, because the *.rs file can't be uploaded normally.

    use ::std::{env, fs, os, path::Path, process};
    #[cfg(windows)]
    fn main() {
        let msys2_home = match env::var("MSYS2_HOME") {
            Ok(value) => value,
            Err(_) => {
                println!("cargo:warning=环境变量`MSYS2_HOME`没有提供,没有链接操作会被执行");
                return;
            }
        };
        println!("调试:MSYS2_HOME={}", msys2_home);
        let out_dir = env::var("OUT_DIR")
            .expect("失败:环境变量`OUT_DIR`未提供");
        println!("调试:OUT_DIR={}", out_dir);
        let exe_dir = Path::new(&out_dir[..]).join("../../..").canonicalize()
            .expect(&format!("失败:不能从 {} 推断出 exe 目录", out_dir)[..]);
        println!("调试:EXE_DIR={}", exe_dir.display());
        if !exe_dir.is_dir() {
            println!("cargo:warning={} 不是一个目录", exe_dir.display());
            process::exit(1);
        }
        let zlib1_exe = exe_dir.join("zlib1.dll");
        println!("调试:ZLIB1_EXE={}", zlib1_exe.display());
        if zlib1_exe.exists() {
            fs::remove_file(zlib1_exe.clone())
                .expect(&format!("失败:不能删除原来的 {} 符号链接文件", zlib1_exe.display())[..]);
        }
        let bits = if cfg!(target_pointer_width = "32") {
            32usize
        } else {
            64usize
        };
        let bin_dir = Path::new(&msys2_home[..]).join(&format!("mingw{}", bits)[..]).join("bin");
        let bin_dir = bin_dir.canonicalize()
            .expect(&format!("失败:不能从 {} 推断出 mingw**/bin 目录", bin_dir.display())[..]);
        println!("调试:BIN_DIR={}", bin_dir.display());
        if !bin_dir.is_dir() {
            println!("cargo:warning={} 不是一个目录", bin_dir.display());
            process::exit(1);
        }
        let zlib1_file = bin_dir.join("zlib1.dll");
        println!("调试:ZLIB1_FILE={}", zlib1_file.display());
        if !zlib1_file.is_file() {
            println!("cargo:warning={} 不是一个文件", zlib1_file.display());
            process::exit(1);
        }
        os::windows::fs::symlink_file(zlib1_file.clone(), zlib1_exe.clone())
            .expect(&format!("失败:不能创建文件链接 {} 指向 {}", zlib1_exe.display(), zlib1_file.display())[..]);
        println!("成功:能创建文件链接 {} 指向 {}", zlib1_exe.display(), zlib1_file.display());
    }
  2. Make a custom environment variable "MSYS2_HOME" which holds the absolute path of the MSYS2 installation directory.

  3. And then, before (cargo & rustc) really compile the rust source code, a soft symbolic link pointing to one of (msys2/mingw64/bin/zlib1.dll) and (msys2/mingw32/bin/zlib1.dll) is always automatically made and named after "zlib1.dll" under the either "target/debug" or "target/release" folder in the project directory. The action is merely taken in Windows OS.

Perfect! Bypass all rules related to the DLL Searching Orders.