Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial for static builds on windows? #581

Closed
jonatino opened this issue Jun 10, 2024 · 5 comments
Closed

Tutorial for static builds on windows? #581

jonatino opened this issue Jun 10, 2024 · 5 comments

Comments

@jonatino
Copy link

jonatino commented Jun 10, 2024

Can't seem to find any guide for building staticically on windows.

#104 (comment)

@twistedfall left a message in that comment with a link to https://github.com/twistedfall/opencv-rust#static-build but it does not exist anymore unfortunately.

My current attempt uses the following env vars

[env]
LIBCLANG_PATH="C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\x64\\bin"
OPENCV_INCLUDE_PATHS="C:\\tools\\opencv\\build\\include"
OPENCV_HEADER_DIR="C:\\tools\\opencv\\build\\include"
OPENCV_LINK_PATHS="C:\\tools\\opencv\\build\\x64\\vc16\\lib"
OPENCV_LINK_LIBS="opencv_world4100.lib"
OPENCV_MSVC_CRT="static"

but it still looks for a dll on startup

@jonatino
Copy link
Author

Possible found the answer here: https://github.com/spoorn/media-to-ascii/blob/a01017fbb3dc883a89b0a1aff237c69b072542b2/.github/workflows/build.yml

Will reopen it if it does not work.

@twistedfall
Copy link
Owner

I've moved the installation guide into a separate file, so this is now the correct link: https://github.com/twistedfall/opencv-rust/blob/master/INSTALL.md#static-build

I haven't really tried building and linking statically on Windows myself, so if you have success please share and I'll link to your comment in the readme, thanks!

@jonatino
Copy link
Author

jonatino commented Jun 14, 2024

@twistedfall ended up getting it to work, using a mix of vcpkg and choco.

I used choco install llvm

I used https://github.com/mcgoo/cargo-vcpkg

Added the following to my Cargo.toml file in the root of the project:

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

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-release", dependencies = ["opencv4[contrib,nonfree]"] }

Then create a cargo config file in project_root/.cargo/config.toml and add the following.

[env]
OPENCV_INCLUDE_PATHS= { value = "target\\vcpkg\\installed\\x64-windows-static-release\\include", relative = true }
OPENCV_LINK_PATHS= { value = "target\\vcpkg\\installed\\x64-windows-static-release\\lib", relative = true }
OPENCV_LINK_LIBS="opencv_core4.lib,opencv_imgproc4.lib,zlib.lib"
OPENCV_MSVC_CRT="static"

Then run cargo vcpkg -v build.

Once that completes you'll be able to use cargo as normal. Eg cargo build, cargo build --release etc.

@adminSxs
Copy link

opencv4[contrib,nonfree]

@twistedfall ended up getting it to work, using a mix of vcpkg and choco.

I used choco install llvm

I used https://github.com/mcgoo/cargo-vcpkg

Added the following to my Cargo.toml file in the root of the project:

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

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-release", dependencies = ["opencv4[contrib,nonfree]"] }

Then create a cargo config file in project_root/.cargo/config.toml and add the following.

[env]
OPENCV_INCLUDE_PATHS= { value = "target\\vcpkg\\installed\\x64-windows-static-release\\include", relative = true }
OPENCV_LINK_PATHS= { value = "target\\vcpkg\\installed\\x64-windows-static-release\\lib", relative = true }
OPENCV_LINK_LIBS="opencv_core4.lib,opencv_imgproc4.lib,zlib.lib"
OPENCV_MSVC_CRT="static"

Then run cargo vcpkg -v build.

Once that completes you'll be able to use cargo as normal. Eg cargo build, cargo build --release etc.

Hello, cargo vcpk -v build success,but i add

[dependencies]
opencv="*"

show below error

  === OpenCV library configuration: Library {
      include_paths: [
          "D:\\projects\\rust\\opencv4\\target\\vcpkg\\installed\\x64-windows-static-release\\include",
      ],
      version: Version {
          major: 0,
          minor: 0,
          patch: 0,
      },
      cargo_metadata: [
          "cargo:rustc-link-search=D:\\projects\\rust\\opencv4\\target\\vcpkg\\installed\\x64-windows-static-release\\lib",
          "cargo:rustc-link-lib=opencv_core4",
          "cargo:rustc-link-lib=opencv_imgproc4",
          "cargo:rustc-link-lib=zlib",
      ],
  }
  thread 'main' panicked at D:\software\cargo\registry\src\rsproxy.cn-8f6827c7555bfaf8\opencv-0.89.0\build.rs:346:9:
  Unsupported OpenCV version: 0.0.0, must be from 3.2, 3.4 or 4.x branch

@jonatino
Copy link
Author

jonatino commented Jun 23, 2024

@adminSxs your version of opencv is outdated. Use 0.92.0

Here is my opencv entry in Cargo.toml

opencv  = { version = "0.92.0", default-features = false,features = ["rgb", "imgproc"] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants