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

statically compiling opencv #364

Closed
rhobro opened this issue Aug 24, 2022 · 16 comments
Closed

statically compiling opencv #364

rhobro opened this issue Aug 24, 2022 · 16 comments

Comments

@rhobro
Copy link

rhobro commented Aug 24, 2022

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

@twistedfall
Copy link
Owner

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!

@rhobro
Copy link
Author

rhobro commented Aug 25, 2022

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
Copy link
Author

rhobro commented Aug 29, 2022

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

@twistedfall
Copy link
Owner

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!

@rhobro
Copy link
Author

rhobro commented Aug 30, 2022

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 rhobro changed the title running opencv on mobile and including opencv statically compiling opencv Aug 30, 2022
@rhobro
Copy link
Author

rhobro commented Aug 30, 2022

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.

@spoorn
Copy link

spoorn commented Sep 21, 2022

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?

@rhobro
Copy link
Author

rhobro commented Sep 21, 2022

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

@twistedfall
Copy link
Owner

twistedfall commented Oct 1, 2022

I'm going to close this issue in favor of the already existing one about static compilation: #104

@jb-alvarado
Copy link

jb-alvarado commented Nov 9, 2022

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
      

@twistedfall
Copy link
Owner

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

@jb-alvarado
Copy link

jb-alvarado commented Nov 10, 2022

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.

@twistedfall
Copy link
Owner

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

@00hello
Copy link

00hello commented Mar 27, 2023

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?

@twistedfall
Copy link
Owner

twistedfall commented Mar 28, 2023

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

@spoorn
Copy link

spoorn commented Nov 6, 2023

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

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

5 participants