opencv-rust
opencv-rust copied to clipboard
statically compiling opencv
Hi there,
I saw on your README that opencv has to be installed on the device for it to run. I am writing an app in flutter (aimed at android, ios, windows, linux, macos) which has bits of rust using opencv. How can I package opencv with the app? This is mostly for the mobile versions which may not have it installed.
Thanks
Probably static linking is a solution, I haven’t tested this option, but there were a couple of successful mentions. And working with mobile devices is also something I haven’t really considered so far. So if you manage to get something working I would really like to hear about it!
Hi there,
I read on the opencv website that the library was adjusted for ios and android. So I think compiling them for the platforms should work?
I am new to rust bindings to C and C++. I'm trying to dig through the build script. How does one go about statically linking opencv?
Thanks @twistedfall
Would it be possible to introduce a feature flag on the crate to decide between static and dynamic linking?
So I think compiling them for the platforms should work?
In theory, yes, but I can't say that I tried that myself
How does one go about statically linking opencv? Would it be possible to introduce a feature flag on the crate to decide between static and dynamic linking?
In theory it's just about the linker flags. If you manage to get something working I would really appreciate the PR. Having a feature flag for switching between dynamic and static linking is a good idea!
I've forked the repo and have edited the RUSTFLAGS. I've been trying to test it out in a local cargo project but it seems to return errors saying that all the probes failed to find opencv.
It is returning errors that environment and vcpkg cannot probe it properly - how can I fix this in the build script?
It seems to work when I build my program for my host but it seems to break when cross-compiling. It says environment and vcpkg cannot probe opencv.
Probably static linking is a solution, I haven’t tested this option, but there were a couple of successful mentions. And working with mobile devices is also something I haven’t really considered so far. So if you manage to get something working I would really like to hear about it!
Are there any examples of how to do the static linking?
I read that changing the LDFLAGS can switch between dynamic and static. But it seems to not include opencv in the dylib.
I'm going to close this issue in favor of the already existing one about static compilation: https://github.com/twistedfall/opencv-rust/issues/104
Hello, I don't know if it fits here, but I wanted to share my steps, to build with static openCV libs. I was not able to use as target x86_64-unknown-linux-musl, I think for that openCV has to compile also with a musl compiler.
In my project I only use features: face and imgproc:
opencv = { version = "0.73", default-features = false, features = ["face", "imgproc"]}
Here are the compiling steps:
- download sources:
-
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip
-
- unzip sources, and delete archive:
-
unzip opencv.zip && rm opencv.zip unzip opencv_contrib.zip && rm opencv_contrib.zip
-
- create build folder and switch to it:
-
mkdir -p build && cd build
-
- configure build
-
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=NO \ -DCMAKE_INSTALL_PREFIX=/opt/opencv -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF \ -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_java=OFF \ -DBUILD_opencv_python=OFF -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules \ ../opencv-4.x
-
- compile and install opencv
-
make -j8 && make install
-
- go to rust project
- compile rust project with:
-
OPENCV_LINK_LIBS="opencv_imgproc,opencv_face,opencv_objdetect,opencv_dnn,opencv_dnn_objdetect,opencv_core,ippiw,ittnotify,ippicv,liblibprotobuf,z" \ OPENCV_LINK_PATHS=/opt/opencv/lib,/opt/opencv/lib/opencv4/3rdparty,/usr/lib/x86_64-linux-gnu \ OPENCV_INCLUDE_PATHS=/opt/opencv/include/opencv4 \ cargo build --release
-
That's really helpful, thank you! I will try to include this step into the CI and update the readme
I will try to include this step into the CI and update the readme
Maybe you should also mention that it is a bit tricky to get the right ordering from libs in OPENCV_LINK_LIBS
, for example when you put libippicv
before libippiw
the compiler doesn't find all dependencies.
With libz I was not sure which one to choice, the static or the dynamic one. The rust program will link against the dynamic library, but openCV I guess will link to the static one.
I've added the static build to CI and updated the readme: https://github.com/twistedfall/opencv-rust#static-build
I've added the static build to CI and updated the readme: https://github.com/twistedfall/opencv-rust#static-build
@twistedfall
Question: I'm trying to statically compile opencv, as well (on Ubuntu focal fossa). I'm not sure which script I'm supposed to run, install-focal.sh or script.sh. Why do you have a linux install script in both; what does one do that the other doesn't?
And if I do run install-focal.sh, how do I run it? Do I run ./install-focal.sh
directly from command line? or do I run ./install.sh
(install.sh, which seems to call install-focal.sh) with $OS_FAMILY environment variable set to Linux?
For installation of the static OpenCV itself on Ubuntu use the install-focal.sh
(Focal stands for Ubuntu Focal Fossa, 20.04 version), just be sure to set up env var OPENCV_VERSION=4.7.0-static
before doing that. The build will be done in $HOME/build/opencv/
directory and installed to /usr
(you might want to change that to /usr/local
or something like this by editing the script before running).
For anyone looking at this in the future, I have a github actions workflow file that statically links OpenCV into my binary for Windows, macOS, and Linux
There are also comments in workflow file to help understand the differences between platforms