mcgoo / vcpkg-rs

Build library for finding native libraries in vcpkg for Rust - Windows (msvc), Linux and macOS
https://docs.rs/vcpkg
Apache License 2.0
108 stars 22 forks source link

Unable to find library in Windows for NotMSVC #22

Closed snakevil closed 3 years ago

snakevil commented 4 years ago

Hi, I ran into some unexplained trouble although following the readmes step by step. The steps below are what I exactly did. How could I solve it?

  1. in Cargo.toml
[dependencies]
vcpkg = "0.2.10"

[package.metadata.vcpkg]
git = "https://github.com/microsoft/vcpkg"
rev = "56fffbe"

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static", install = ["tesseract"] }
  1. cargo vcpkg build

  2. vcpkg_cli probe -l static tesseract

Found library tesseract
Include paths:
  D:\x\target\vcpkg\installed\x64-windows-static\include
Library paths:
  D:\x\target\vcpkg\installed\x64-windows-static\lib
Runtime Library paths:
Cargo metadata:
  cargo:rustc-link-search=native=D:\x\target\vcpkg\installed\x64-windows-static\lib
  cargo:rustc-link-lib=tesseract41
  cargo:rustc-link-lib=leptonica-1.78.0
  cargo:rustc-link-lib=zlib
  cargo:rustc-link-lib=tiff
  cargo:rustc-link-lib=tiffxx
  cargo:rustc-link-lib=lzma
  cargo:rustc-link-lib=jpeg
  cargo:rustc-link-lib=turbojpeg
  cargo:rustc-link-lib=libwebpmux
  cargo:rustc-link-lib=webp
  cargo:rustc-link-lib=webpdecoder
  cargo:rustc-link-lib=webpdemux
  cargo:rustc-link-lib=libpng16
  cargo:rustc-link-lib=gif
Found libs:
  D:\x\target\vcpkg\installed\x64-windows-static\lib\tesseract41.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\leptonica-1.78.0.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\zlib.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\tiff.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\tiffxx.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\lzma.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\jpeg.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\turbojpeg.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\libwebpmux.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\webp.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\webpdecoder.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\webpdemux.lib
  D:\x\target\vcpkg\installed\x64-windows-static\lib\gif.lib
Libraries linking names:
  tesseract41
  leptonica-1.78.0
  tiff
  tiffxx
  lzma
  jpeg
  libwebpmux
  webp
  webpdecoder
  webpdemux
  libpng16
  gif
  1. in main.rs
fn main() {
    vcpkg::find_package("tesseract").unwrap();
}
  1. cargo run
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotMSVC', src\main.rs:2:38
mcgoo commented 4 years ago

Because of the requirements of Windows when it comes to C runtime selection, you need to do one of the following

There is a little more info here.

The NotMSVC error message is wrong - sorry about that.

snakevil commented 4 years ago

Thanks so much for your advice!

I changed x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", install = ["tesseract"] } in the Cargo.toml, and ran cargo vcpkg build once more. However the same error message occurred on cargo run, even with $RUSTFLAGS="-Ctarget-feature=+crt-static".

Did I understand something wrong?

Once again, thanks for your immediate reply!

mcgoo commented 4 years ago

Hmm, maybe the NotMSVC is not spurious after all! Are you actually using the MSVC Rust toolchain, or do you have the -gnu version selected? (rustup show)

At this point I have not tried vcpkg with mingw - in principle it is possible since a vcpkg triplet community triplet exists. Vcpkg itself was, last time I checked, not yet able to build itself with mingw so an msvc install would be required anyway.

snakevil commented 4 years ago

It was so hard to setup a development environment in Windows!

I installed rust and rustup through scoop, for it is more like brew which I'm used to. And I also installed Visual Studio 2017 Build Tools, to use the MSVC ABI without Visual Studio 2015 (or higher) installed.

  1. scoop info rustup
Name: rustup
Description: Manage multiple rust installations with ease
Version: 1.22.1
Website: https://rustup.rs
License: MIT|Apache-2.0 (https://spdx.org/licenses/MIT.html, https://spdx.org/licenses/Apache-2.0.html)
Manifest:
  C:\Users\Snakevil\scoop\buckets\main\bucket\rustup.json
Installed:
  C:\Users\Snakevil\scoop\apps\rustup\1.22.1
Environment:
  CARGO_HOME=C:\Users\Snakevil\scoop\persist\rustup\.cargo
  RUSTUP_HOME=C:\Users\Snakevil\scoop\persist\rustup\.rustup
  PATH=%PATH%;C:\Users\Snakevil\scoop\apps\rustup\1.22.1\.cargo\bin
Notes
-----
To use the MSVC ABI without Visual Studio 2015 (or higher) installed, you will need the Visual Studio 2017 Build Tools: https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017
  1. scoop info rust
Name: rust
Description: A language empowering everyone to build reliable and efficient software. (GNU toolchain)
Version: 1.46.0
Website: https://www.rust-lang.org
License: MIT|Apache-2.0 (https://spdx.org/licenses/MIT.html, https://spdx.org/licenses/Apache-2.0.html)
Manifest:
  C:\Users\Snakevil\scoop\buckets\main\bucket\rust.json
Installed:
  C:\Users\Snakevil\scoop\apps\rust\1.46.0
Binaries:
  bin\rustc.exe bin\rustdoc.exe bin\cargo.exe
  1. rustup show
Default host: x86_64-pc-windows-msvc
rustup home:  C:\Users\Snakevil\scoop\persist\rustup\.rustup

stable-x86_64-pc-windows-msvc (default)
rustc 1.46.0 (04488afe3 2020-08-24)
mcgoo commented 4 years ago

That all looks good.

Looking again, I see that you are running find_package from main.rs. You need to put vcpkg = "0.2.10" under [build-dependencies] and run it from build.rs.

Thanks for the great detail in the issue. I should have spotted that the first time round.

snakevil commented 4 years ago

OK. I appended this new section to Cargo.toml as:

[dependencies]
vcpkg = "0.2.10"

[build-dependencies]
vcpkg = "0.2.10"

[package.metadata.vcpkg]
git = "https://github.com/microsoft/vcpkg"
rev = "56fffbe"

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", install = ["tesseract"] }

But the error still occurs on cargo run. Hope the following backtrace could expose something useful.

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotMSVC', src\main.rs:2:38
stack backtrace:
   0: backtrace::backtrace::trace_unsynchronized
             at C:\Users\runneradmin\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.46\src\backtrace\mod.rs:66
   1: std::sys_common::backtrace::_print_fmt
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\sys_common\backtrace.rs:78
   2: std::sys_common::backtrace::_print::{{impl}}::fmt
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\sys_common\backtrace.rs:59
   3: core::fmt::write
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libcore\fmt\mod.rs:1076
   4: std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\io\mod.rs:1537
   5: std::sys_common::backtrace::_print
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\sys_common\backtrace.rs:62
   6: std::sys_common::backtrace::print
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\sys_common\backtrace.rs:49
   7: std::panicking::default_hook::{{closure}}
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:198
   8: std::panicking::default_hook
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:217
   9: std::panicking::rust_panic_with_hook
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:526
  10: std::panicking::begin_panic_handler
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:437
  11: core::panicking::panic_fmt
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libcore\panicking.rs:85
  12: core::option::expect_none_failed
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libcore\option.rs:1269
  13: core::result::Result<vcpkg::Library, vcpkg::Error>::unwrap<vcpkg::Library,vcpkg::Error>
             at C:\Users\Snakevil\scoop\persist\rustup\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\src\libcore\result.rs:1005
  14: x::main
             at .\src\main.rs:2
  15: std::rt::lang_start::{{closure}}<tuple<>>
             at C:\Users\Snakevil\scoop\persist\rustup\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\src\libstd\rt.rs:67
  16: std::rt::lang_start_internal::{{closure}}
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\rt.rs:52
  17: std::panicking::try::do_call
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:348
  18: std::panicking::try
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panicking.rs:325
  19: std::panic::catch_unwind
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\panic.rs:394
  20: std::rt::lang_start_internal
             at /rustc/04488afe34512aa4c33566eb16d8c912a3ae04f9\/src\libstd\rt.rs:51
  21: std::rt::lang_start<tuple<>>
             at C:\Users\Snakevil\scoop\persist\rustup\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\src\libstd\rt.rs:67
  22: main
  23: invoke_main
             at D:\agent\_work\9\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  24: __scrt_common_main_seh
             at D:\agent\_work\9\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  25: BaseThreadInitThunk
  26: RtlUserThreadStart
mcgoo commented 4 years ago

Looks like you need to delete it from main.rs?

snakevil commented 4 years ago

Sorry, I don't get your point here. Moved this code snippet into another file, did not change anything.

mcgoo commented 4 years ago

You need to add a build script.

The build script is an optional program that will be run before the main code in the crate is compiled. It goes in a file called build.rs at the root of the package - the same level as Cargo.toml. The build script gets called by cargo with some extra enviroment variables set that relate to the main code in your package.

The vcpkg build script helper crate that you are calling find_package from requires the extra (build step) environment variables in order to work. It cannot not work without them, so it cannot work from application code in src/main.rs, only build script code in build.rs.

Take a look at the build script in the tesseract-sys crate which is used by the tesseract crate for a working example.

snakevil commented 4 years ago

OK. I'll read the handbook carefully once again. Thank you very much for your patience and guidance!