Categories Generic

How to install OpenCV crate for rust on MacOS

All of us have had some trouble setting up the OpenCV crate for Rust-lang on macOS. In this article, I will be showing you how to properly set up OpenCV so that when you build it, you do not encounter any build issues.

opencv crate :

Note: In this article, we will rely on auto-detection of the OpenCV library, which is the best thing to do on macOS. So make sure to only set the environment variables that have been advised here. You do not need to set any variables that relate to OpenCV specifically

Step 1: Install OpenCV using HomeBrew

You must install OpenCV using HomeBrew. Trust me, this will do the heavy lifting. if you do not use HomeBrew, then use it.

You can download it from here:

Run the following Command in a terminal and wait for it to finish. Do not worry about setting up the environment variables or anything. I will give you the environment variables and what exactly you need to set them to.

brew install opencv

Step 2: Install llvm using HomeBrew

Another very important thing you need is llvm. Again, you just need to run the following command, and the rest will be taken care of. Just wait for it to finish and then go to the next step.

brew install llvm

Step 3: Setup Environment Variables

Make sure to add the following variables to your ~/.zshrc or ~/.bashrc file, depending on which one you use: If you are unsure which one to use, just go with the ~/.zshrc.

In a terminal run the following command

nano ~/.zshrc

Paste the following three lines at the end of your ~/.zshrc

export DYLD_FALLBACK_LIBRARY_PATH="$(xcode-select --print-path)/Toolchains/XcodeDefault.xctoolchain/usr/lib/"
export LDFLAGS=-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

I have set these up in a dynamic manner, so do not worry about changing anything for your OS.

Step 4: Make sure to properly source the ~/.zshrc file

We’re done now. Before you decide to compile your project again, make sure that Terminal has properly sourced the updated ~/.zshrc file.

You can do that by either restarting the terminal or running the following command

source ~/.zshrc

Now you should be able to build that open-CV crate.

Leave a Reply

Your email address will not be published. Required fields are marked *