OpenCV and CUDA - updated build notes

Yes, this comes up a lot, and if you're getting mysterious build breaks, maybe it's a mismatch between what version of gcc/g++ CUDA requires, your default compiler.

The symptoms are that cmake completes just fine (so it found a compiler, all the libraries, etc) but then you get a "failed to compile" error when you do a "make -j[number]"

The first thing to do is to run a single-instance make, because the errors will likely be less obtuse.  When I did this, I got:

"error: #error -- unsupported GNU version! gcc versions later than 8 are not supported!"

gcc --version told me it was version 9, so there's the problem.  But how to get around it?

  1. Make sure you have a suitable version of gcc and g++ installed (version 8) in our case.  On Ubuntu, you can do this with:
    "sudo apt install -y gcc-8 g++-8"
  2. Tell cmake about it when you do the pre-build with:
    "-D CMAKE_C_COMPILER=gcc-8 -D CMAKE_CXX_COMPILER=g++-8"
To wrap up, the cmake command line looks like this:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D HAVE_opencv_python3=ON \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D CMAKE_C_COMPILER=gcc-8 \
-D CMAKE_CXX_COMPILER=g++-8 \
-D BUILD_EXAMPLES=ON ..

This command makes some assumptions I should point out:

  1. You have already installed and verified the pre-requisites.
  2. The opencv and opencv_contrib source trees have been fully expanded and start at the same level under your source.
  3. You created a build directory within the opencv source, and you're executing from within that directory.

Hope this helps you out!


Wasting your and my time

I had a really interesting experience recently which I hope might enlighten others as much as it did me: I was approached (via LinkedIn) by ...