How to Solve an Error While Installing OpenCV on Linux

I was trying to install OpenCV on my Linux platform, and I followed an online guide that seemed pretty straightforward. The guide walked me through the installation steps, but as soon as I tried to build the samples, I encountered a frustrating error. The error message looked like this:

compiling contours.c
  /usr/bin/ld: cannot find -lcufft
  /usr/bin/ld: cannot find -lnpps
  /usr/bin/ld: cannot find -lnppi
  /usr/bin/ld: cannot find -lnppc

At first, I thought this might be a minor issue, but after some troubleshooting, I realized it was tied to CUDA libraries, which are essential for OpenCV’s GPU-accelerated operations. If you’re facing the same issue, here’s what I discovered and how I solved it.

What Is the Error

The error message above suggests that the linker (ld) cannot find certain CUDA libraries that OpenCV needs to build and run its sample applications. These libraries include:

  • -lcufft (CUDA Fast Fourier Transform library)
  • -lnpps (CUDA NPP library)
  • -lnppi (NVIDIA Performance Primitives)
  • -lnppc (CUDA NPP for color conversion)

These libraries are part of the CUDA toolkit, and the error indicates that although these libraries are installed (in my case, in /usr/local/cuda-7.5/), the system cannot locate them during the linking stage of the OpenCV build process. The root cause could be one of several factors that I’ll explain below.

Common Causes of the Error

Here are a few reasons why this error might happen:

CUDA Installation Path Issue

Even though the libraries are located in /usr/local/cuda-7.5/, the system may not be able to find them due to incorrect or missing environment variables. These variables, like LD_LIBRARY_PATH and CUDA_HOME, are crucial for ensuring the system knows where the CUDA libraries are.

Missing CUDA Development Libraries

If you’re using a version of CUDA that doesn’t include the necessary development libraries, OpenCV’s build process will fail to link correctly. This could happen if you’ve only installed the runtime version of CUDA, which lacks these libraries.

OpenCV CUDA Modules Not Enabled

In some cases, OpenCV might not have been configured with CUDA support when you installed it. Without CUDA support, OpenCV won’t be able to link to the necessary CUDA libraries.

The Solution

Check Your CUDA Installation

Before anything else, make sure that CUDA is installed properly. To check, run the following command:

nvcc --version

This should return the version of CUDA installed on your system. If this command doesn’t work, it means CUDA might not be installed correctly.

Verify Environment Variables

Next, ensure that your environment variables are set up correctly. These environment variables tell the system where to find the CUDA libraries. To set them, open your .bashrc (or .zshrc if using Zsh) and add the following lines:

export CUDA_HOME=/usr/local/cuda-7.5
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH

Then, reload your shell to apply the changes:

source ~/.bashrc

Rebuild OpenCV with CUDA Support

If OpenCV wasn’t originally built with CUDA support, you’ll need to reconfigure and rebuild it. When configuring OpenCV with cmake, make sure the following flags are enabled:

cmake -D WITH_CUDA=ON -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-7.5 ..

This ensures that CUDA is enabled during the build process.

Install Missing Libraries

It’s also possible that the libraries libcufft, libnpps, libnppi, and libnppc are missing or not installed by default. To install the necessary development libraries, run:

sudo apt-get install nvidia-cuda-toolkit

This will install the required libraries and tools for CUDA development.

Linking the CUDA Libraries

Sometimes, even after installation, the libraries may not be properly linked. In such cases, you can manually add the following flags to the OpenCV linking process:

-L/usr/local/cuda-7.5/lib64 -lcufft -lnpps -lnppi -lnppc

Example Code with CUDA Integration

Now that we’ve tackled the installation problem, let’s move on to an example of how to use CUDA with OpenCV. Here’s a simple example that uses CUDA for matrix multiplication:

#include <opencv2/opencv.hpp>
#include <opencv2/cuda.hpp>

int main()
{
    // Create two matrices using OpenCV
    cv::Mat A = (cv::Mat_<float>(2, 2) << 1, 2, 3, 4);
    cv::Mat B = (cv::Mat_<float>(2, 2) << 5, 6, 7, 8);

    // Allocate GPU memory
    cv::cuda::GpuMat d_A, d_B, d_C;
    d_A.upload(A);
    d_B.upload(B);

    // Perform matrix multiplication on GPU using CUDA
    cv::cuda::multiply(d_A, d_B, d_C);

    // Download result back to CPU
    cv::Mat C;
    d_C.download(C);

    std::cout << "Matrix C:\n" << C << std::endl;

    return 0;
}

Explanation of the Code:

  1. Matrix Creation:
    I create two 2×2 matrices A and B using OpenCV’s Mat class.
  2. GPU Memory Allocation:
    I allocate memory on the GPU using OpenCV’s CUDA module (cv::cuda::GpuMat). The upload function transfers the matrices to the GPU.
  3. Matrix Multiplication:
    I perform matrix multiplication using the cv::cuda::multiply function, which automatically executes the operation on the GPU.
  4. Download Results:
    Finally, the result is downloaded back to the CPU memory using the download function and printed to the console.

Additional Troubleshooting Tips:

  • CUDA Compatibility: Ensure that your CUDA version is compatible with the version of OpenCV you are using.
  • Update GPU Drivers: Sometimes, updating your NVIDIA drivers can resolve issues related to CUDA and GPU compatibility.
  • Use Precompiled OpenCV with CUDA: If you are still facing issues, consider using a precompiled OpenCV package with CUDA support to simplify the installation.

Final Thoughts

I learned a lot through this process. OpenCV’s CUDA support is a great way to accelerate image processing tasks, but it requires careful configuration of both OpenCV and CUDA. By following the steps above, I was able to solve the linker error and successfully build OpenCV with CUDA support. If you are encountering the same issue, I hope this guide helps you get your project up and running.

Related blog posts