opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

statically compiling opencv

Open rhobro opened this issue 2 years ago • 6 comments

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

rhobro avatar Aug 24 '22 18:08 rhobro

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!

twistedfall avatar Aug 24 '22 19:08 twistedfall

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

rhobro avatar Aug 25 '22 18:08 rhobro

Would it be possible to introduce a feature flag on the crate to decide between static and dynamic linking?

rhobro avatar Aug 29 '22 19:08 rhobro

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!

twistedfall avatar Aug 30 '22 07:08 twistedfall

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?

rhobro avatar Aug 30 '22 11:08 rhobro

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.

rhobro avatar Aug 30 '22 11:08 rhobro

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?

spoorn avatar Sep 21 '22 09:09 spoorn

I read that changing the LDFLAGS can switch between dynamic and static. But it seems to not include opencv in the dylib.

rhobro avatar Sep 21 '22 11:09 rhobro

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

twistedfall avatar Oct 01 '22 07:10 twistedfall

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:

  1. 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
      
  2. unzip sources, and delete archive:
    •   unzip opencv.zip && rm opencv.zip
        unzip opencv_contrib.zip && rm opencv_contrib.zip
      
  3. create build folder and switch to it:
    •   mkdir -p build && cd build
      
  4. 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
      
  5. compile and install opencv
    •   make -j8 && make install
      
  6. go to rust project
  7. 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
      

jb-alvarado avatar Nov 09 '22 13:11 jb-alvarado

That's really helpful, thank you! I will try to include this step into the CI and update the readme

twistedfall avatar Nov 09 '22 16:11 twistedfall

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.

jb-alvarado avatar Nov 10 '22 06:11 jb-alvarado

I've added the static build to CI and updated the readme: https://github.com/twistedfall/opencv-rust#static-build

twistedfall avatar Dec 13 '22 15:12 twistedfall

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?

00hello avatar Mar 27 '23 20:03 00hello

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).

twistedfall avatar Mar 28 '23 08:03 twistedfall

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

spoorn avatar Nov 06 '23 18:11 spoorn