CUDA incompatible with my gcc version

As already pointed out, nvcc depends on gcc 4.4. It is possible to configure nvcc to use the correct version of gcc without passing any compiler parameters by adding softlinks to the bin directory created with the nvcc install.

The default cuda binary directory (the installation default) is /usr/local/cuda/bin, adding a softlink to the correct version of gcc from this directory is sufficient:

sudo ln -s /usr/bin/gcc-4.4 /usr/local/cuda/bin/gcc


  1. Check the maximum supported GCC version for your CUDA version:

    CUDA version max supported GCC version
    11.4.1+, 11.5, 11.6 11
    11.1, 11.2, 11.3, 11.4.0 10
    11 9
    10.1, 10.2 8
    9.2, 10.0 7
    9.0, 9.1 6
    8 5.3
    7 4.9
    5.5, 6 4.8
    4.2, 5 4.6
    4.1 4.5
    4.0 4.4
  2. Set an env var for that GCC version. For example, for CUDA 10.2:

    MAX_GCC_VERSION=8
    
  3. Make sure you have that version installed:

    sudo apt install gcc-$MAX_GCC_VERSION g++-$MAX_GCC_VERSION
    
  4. Add symlinks within CUDA folders:

    sudo ln -s /usr/bin/gcc-$MAX_GCC_VERSION /usr/local/cuda/bin/gcc 
    sudo ln -s /usr/bin/g++-$MAX_GCC_VERSION /usr/local/cuda/bin/g++
    

    (or substitute /usr/local/cuda with your CUDA installation path, if it's not there)

See this GitHub gist for more information on the CUDA-GCC compatibility table.


gcc 4.5 and 4.6 are not supported with CUDA - code won't compile and the rest of the toolchain, including cuda-gdb, won't work properly. You cannot use them, and the restriction is non-negotiable.

Your only solution is to install a gcc 4.4 version as a second compiler (most distributions will allow that). There is an option to nvcc --compiler-bindir which can be used to point to an alternative compiler. Create a local directory and then make symbolic links to the supported gcc version executables. Pass that local directory to nvcc via the --compiler-bindir option, and you should be able to compile CUDA code without affecting the rest of your system.


EDIT:

Note that this question, and answer, pertain to CUDA 4.

Since it was written, NVIDIA has continued to expand support for later gcc versions in newer CUDA toolchain release

  • As of the CUDA 4.1 release, gcc 4.5 is now supported. gcc 4.6 and 4.7 are unsupported.
  • As of the CUDA 5.0 release, gcc 4.6 is now supported. gcc 4.7 is unsupported.
  • As of the CUDA 6.0 release, gcc 4.7 is now supported.
  • As of the CUDA 7.0 release, gcc 4.8 is fully supported, with 4.9 support on Ubuntu 14.04 and Fedora 21.
  • As of the CUDA 7.5 release, gcc 4.8 is fully supported, with 4.9 support on Ubuntu 14.04 and Fedora 21.
  • As of the CUDA 8 release, gcc 5.3 is fully supported on Ubuntu 16.06 and Fedora 23.
  • As of the CUDA 9 release, gcc 6 is fully supported on Ubuntu 16.04, Ubuntu 17.04 and Fedora 25.
  • The CUDA 9.2 release adds support for gcc 7
  • The CUDA 10.1 release adds support for gcc 8
  • The CUDA 10.2 release continues support for gcc 8
  • The CUDA 11.0 release adds support for gcc 9 on Ubuntu 20.04
  • The CUDA 11.1 release expands gcc 9 support across most distributions and adds support for gcc 10 on Fedora linux

There is presently (as of CUDA 11.1) no gcc 10 support in CUDA other than Fedora linux

Note that NVIDIA has recently added a very useful table here which contains the supported compiler and OS matrix for the current CUDA release.


Gearoid Murphy's solution works better for me since on my distro (Ubuntu 11.10), gcc-4.4 and gcc-4.6 are in the same directory, so --compiler-bindir is no help. The only caveat is I also had to install g++-4.4 and symlink it as well:

sudo ln -s /usr/bin/gcc-4.4 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-4.4 /usr/local/cuda/bin/g++

For CUDA7.5 these lines work:

sudo ln -s /usr/bin/gcc-4.9 /usr/local/cuda/bin/gcc 
sudo ln -s /usr/bin/g++-4.9 /usr/local/cuda/bin/g++