146

I have troubles compiling some of the examples shipped with CUDA SDK. I have installed the developers driver (version 270.41.19) and the CUDA toolkit, then finally the SDK (both the 4.0.17 version).

Initially it didn't compile at all giving:

error -- unsupported GNU version! gcc 4.5 and up are not supported!

I found the line responsible in 81:/usr/local/cuda/include/host_config.h and changed it to:

//#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4)
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)

from that point on I got only a few of the examples to compile, it stops with:

In file included from /usr/include/c++/4.6/x86_64-linux-gnu/bits/gthr.h:162:0,
             from /usr/include/c++/4.6/ext/atomicity.h:34,
             from /usr/include/c++/4.6/bits/ios_base.h:41,
             from /usr/include/c++/4.6/ios:43,
             from /usr/include/c++/4.6/ostream:40,
             from /usr/include/c++/4.6/iterator:64,
             from /usr/local/cuda/include/thrust/iterator/iterator_categories.h:38,
             from /usr/local/cuda/include/thrust/device_ptr.h:26,
             from /usr/local/cuda/include/thrust/device_malloc_allocator.h:27,
             from /usr/local/cuda/include/thrust/device_vector.h:26,
             from lineOfSight.cu:37:
/usr/include/c++/4.6/x86_64-linux-gnu/bits/gthr-default.h:251:1: error: pasting         "__gthrw_" and "/* Android's C library does not provide pthread_cancel, check for
`pthread_create' instead.  */" does not give a valid preprocessing token
make[1]: *** [obj/x86_64/release/lineOfSight.cu.o] Error 1

As some of the examples compile I reckon this is not a driver problem, but rather must have something to do with an unsupported gcc version. Downgrading is not an option as gcc4.6 has a whole system as a dependency at this point...

talonmies
  • 70,661
  • 34
  • 192
  • 269
fbielejec
  • 3,492
  • 4
  • 27
  • 35
  • 4
    For future readers: Make sure you're using the latest version of CUDA (unless you absolutely have to use an early one). NVIDIA ups the maximum supported compiler version with almost every release. – einpoklum Aug 03 '15 at 21:56
  • This may be helpful for those with CUDA 10 and getting the error of a too high a gnu compiler chain version: https://stackoverflow.com/questions/53344283/gcc-versions-later-than-7-are-not-supported-by-cuda-10-qt-error-in-arch-linux/53828864#53828864 – Douglas Daseeco Dec 18 '18 at 08:27
  • While installing detectron2 I got a similar message, for that I passed the system variable `export TORCH_DONT_CHECK_COMPILER_ABI=1` and re-ran the setup.py and everything was installed smoothly. I am on CUDA 12.0 g++ 12.x – Muneeb Ahmad Khurram Dec 04 '22 at 19:20

21 Answers21

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

    CUDA version max supported GCC version
    12.1, 12.2 12.2
    12 12.1
    11.4.1+, 11.5, 11.6, 11.7, 11.8 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.

bryant1410
  • 5,540
  • 4
  • 39
  • 40
  • 5
    Saved my life lol configuration nightmare!!!! thank you. I applied this to cuda 10 with gcc and g++ 7 system links. For anyone that comes across this. – thekevshow Apr 03 '19 at 06:13
  • 1
    Should I create the `/usr/bin/gcc` and `/usr/bin/g++` or `/usr/local/cuda/bin/gcc` folders myself? – Josh Desmond May 13 '20 at 16:07
  • @JoshDesmond the symlink for the files you mentioned are created in step 4. – bryant1410 May 13 '20 at 21:36
  • 2
    @bryant1410 When I ran the commands in step four, I remember getting an error along the lines of, "Error: directory /usr/local/cuda/bin/gcc does not exist, aborting," or something similar. I'm realizing now, (after reading the details of the question), that your answer assumes a step 0 mentioned by OP: "I have installed the CUDA toolkit, then finally the SDK". I was trying to do the installation with NVIDIA's `cuda_10.2.89_440.33.01_linux.run` wizard thingy, which simply failed on runtime with a complaint about gcc compatibility. I ended up just deciding to uninstall gcc 9 :P – Josh Desmond May 14 '20 at 04:34
  • Oh, I see. For step 4, you can substitute `/usr/local/cuda` for wherever CUDA is installed. – bryant1410 May 14 '20 at 20:41
  • 1
    If you installed NVCC with [ana|mini]conda (conda-forge package `cudatoolkit-dev`), then you need to link inside your env like `ln -s /usr/bin/gcc-8 /home/user/miniconda3/envs/your_env/bin/gcc` and `ln -s /usr/bin/g++-8 /home/user/miniconda3/envs/your_env/bin/g++` – Diego Ferri Jul 08 '20 at 16:42
  • I think the table would be a good fit here: https://en.wikipedia.org/wiki/Planned_obsolescence (perhaps contrasted with Ubuntu LTS support range?) – mirekphd Dec 13 '20 at 14:00
  • No need to mess up with links around, you can pass to nvcc the compiler to use, example: nvcc -ccbin /usr/bin/g++-10 – Gaetano Mendola Dec 15 '21 at 21:30
  • sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8 – Celso França Nov 26 '22 at 18:02
  • When I do this, I get many `/usr/bin/ld` errors that it can't find `-lcublas`, `-lculibos`, `-lcudart`, `-lcublasLt`. – Geremia Jul 25 '23 at 23:52
  • The gcc-8 package has been discontinued in the Ubuntu 22.04 – Adam Aug 04 '23 at 15:03
147

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

Matt
  • 74,352
  • 26
  • 153
  • 180
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86
  • resolved by writing this comment: I had another cuda install in /development/cuda/ which I actually use... – Dirk Aug 16 '12 at 09:10
  • 2
    "update-alternatives" command may also help, especially when installing CUDA 5.0 – phoad Jan 07 '13 at 21:37
  • 4
    I also had to add a symbolic link to the correct version of g++. – Auron Sep 06 '13 at 15:52
  • 17
    I also had to link to g++. Otherwise, simple `nvcc` invocations work, but say, applying `make` to the CUDA Samples, soon brings in invocations starting with: `nvcc -ccbin g++`. For me I used `sudo ln -s /usr/bin/gcc-4.9 /usr/local/cuda/bin/gcc` and `sudo ln -s /usr/bin/g++-4.9 /usr/local/cuda/bin/g++`. – user2023370 Dec 21 '15 at 12:32
  • 8
    If you compile with `cmake .. && make` you can [try](http://stackoverflow.com/questions/34403447/caffe-compilation-fails-due-to-unsupported-gcc-compiler-version) `cmake -D CUDA_NVCC_FLAGS="-ccbin gcc-4.4" .. && make`. If you use plain `Makefile` you can [try](http://stackoverflow.com/questions/3698441/how-to-use-an-older-version-of-gcc-in-linux) `make CXX=g++-4.4 CC=gcc-4.4`. – patryk.beza Apr 04 '16 at 18:54
  • At this moment, this answer is still relevant. Although now 4.9 must be used with blender 2.77a – twicejr Apr 12 '16 at 18:04
  • 1
    when I try this command, it says "File exists" and doesn't perform the link. Any help ? – Sentient07 May 25 '16 at 20:28
  • This answer doesn't work for me because I don't have installed gcc version 4.4. On dir /usr/bin there is a gcc bin, not gcc-4.4. – VansFannel Jul 01 '16 at 08:26
  • This is great (!), but also requires `ln -s /usr/bin/g++-4.9 g++` to work with old Caffe-based builds that use earlier versions of CUDA. – Brent Faust Apr 27 '18 at 22:06
  • Is it possible to solve this problem without root access? That is, can I somehow set my path variable to point to some older gcc version installed elsewhere local to make the CUDA installer run (I just need to install the libraries, not the drivers)? – ShnitzelKiller Aug 18 '21 at 01:03
65

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.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • Any idea what is used for CUDA 7.5? – GuySoft Jan 13 '16 at 11:53
  • 2
    I use CUDA 7.5 with `gcc 4.9.3` on SLES 11 SP3 without any problem. – Peter VARGA Feb 06 '16 at 13:26
  • 3
    What? How is a code supposed to not compile with higher versions *(except for hardcoded limitations of course)*? The only thing I can think of is that since some version there's C11/C++11 are enabled by default, but if that is causing a problem with an old code, that could be easy workarounded with a command line switch. – Hi-Angel Feb 11 '16 at 11:38
  • @Hi-Angel: The CUDA toolchain relies heavily on the host (in this case gcc) preprocessor and compiler headers and internals to build running host and device code. Those internal implementation details change from gcc version to gcc version, and they often break NVIDIAs own compiler driver and parser in the process. It is for that reason that every CUDA toolkit version has a very carefully defined list of supported gcc versions. It is a real limitation, whether you care to believe it exists, or not – talonmies Feb 11 '16 at 11:51
  • @talonmies I do believe that this is possible to write code in that way, but why? They should have a really big reason for that. – Hi-Angel Feb 11 '16 at 12:17
  • You don't have to create a seperate directory, in order to use the `--compiler-bindir function`. This will also do: `nvcc hello_world.cu --compiler-bindir /usr/bin/g++-4.9 -o hello_world` – Martin R. Mar 17 '16 at 15:05
  • minus one for cuda (not for your answer) for a horrible user experience – Dean Apr 07 '16 at 11:09
  • 2
    Seconding @Hi-Angel. #talonmies what does "the restriction is non-negotiable" even mean? Newer versions of gcc and gdb support older binary headers for object files, as they "always" (sort of) have, there's no reason newer gcc versions shouldn't work. Symlinking solutions aside, any other problem is most likely a c preprocessor version flag setting, and if the gcc version test is "hardcoded" in some cuda header as part of a define or macro, it is easy enough to fix. The exception could be the cuda gpu compiler itself. – Beracah Nov 28 '16 at 19:35
  • 1
    This isn't s binary compatibility question. The CUDA toolchain requires that nvcc and the GPU front end parser can intercept and overload various compiler and libc/libc++ internal headers to both compile host and device code and integrate them together. The CUDA parser needs to be able to parse the gcc internal headers correctly, amongst other things. Untested gcc versions can and do fail, irrespective of preprocessor guards built into the NVIDIA headers. You can either believe me (as someone who has been hacking on the CUDA toolchain for almost 10 years), or not. At this point I don't really – talonmies Nov 28 '16 at 20:33
  • gcc/g++ 5.4 (the default) seems to work fine for CUDA 8.0 in Ubuntu 16.04. – ubfan1 Feb 01 '17 at 20:27
25

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++
Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • Can you please take a look https://stackoverflow.com/questions/74596307/how-to-calculate-the-output-node-of-feed-forward-neural-network-in-cuda question – Encipher Nov 29 '22 at 21:26
12

If using cmake for me none of the hacks of editing the files and linking worked so I compiled using the flags which specify the gcc/g++ version.
cmake -DCMAKE_C_COMPILER=gcc-6 -DCMAKE_CXX_COMPILER=g++-6 ..

Worked like charm.

markroxor
  • 5,928
  • 2
  • 34
  • 43
  • haha I was going to try to link your answer from the other question here because I thought it needed to be put on this thread. Good work again! Thanks – MikeDoho Jul 24 '18 at 02:46
  • One should refrain from posting duplicate answers on SO but I had no option. :) – markroxor Jul 24 '18 at 06:38
11

Check out how to use "update-alternatives" to get around this issue:

... If you install gcc 4.6 you can also use the update-alternatives command to allow for easily switching between versions. This can be configured with:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 
sudo update-alternatives --config gcc
Leigh
  • 28,765
  • 10
  • 55
  • 103
Akanes
  • 477
  • 1
  • 4
  • 11
11

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++
neouyghur
  • 1,577
  • 16
  • 31
6

On most distributions you have the possibility to install another gcc and g++ version beside a most recent compiler like gcc-4.7. In addition most build systems are aware of the CC and CXX environment variables, which let specify you other C and C++ compilers respectively. SO I suggest something like:

CC=gcc-4.4 CXX=g++-4.4 cmake path/to/your/CMakeLists.txt

For Makefiles there should be a similar way. I do not recommend setting custom symlinks within /usr/local unless you know what you are doing.

math
  • 8,514
  • 10
  • 53
  • 61
3

This works for fedora 23. The compat gcc repositories will be slightly different based on your version of fedora.

If you install the following repositories:

sudo yum install compat-gcc-34-c++-3.4.6-37.fc23.x86_64 compat-gcc-34-3.4.6-37.fc23.x86_64 

Now make the soft links as mentioned above assuming your cuda bin folder is in /usr/local/cuda/

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

You should now be able to compile with nvcc without the gcc version error.

Chris Chaffin
  • 61
  • 1
  • 7
2

Gearoid Murphy's solution works like a charm. For me I had two directories for cuda -

/usr/local/cuda 
/usr/local/cuda-5.0

The soft links had to be added only to the directory mentioned below -

/usr/local/cuda 

Also, both g++ and gcc soft links were required as mentioned by SchighSchagh.

arpwal
  • 895
  • 1
  • 9
  • 17
2

Another way of configuring nvcc to use a specific version of gcc (gcc-4.4, for instance), is to edit nvcc.profile and alter PATH to include the path to the gcc you want to use first.

For example (gcc-4.4.6 installed in /opt):

PATH += /opt/gcc-4.4.6/lib/gcc/x86_64-unknown-linux-gnu/4.4.6:/opt/gcc-4.4.6/bin:$(TOP)/open64/bin:$(TOP)/share/cuda/nvvm:$(_HERE_):

The location of nvcc.profile varies, but it should be in the same directory as the nvcc executable itself.

This is a bit of a hack, as nvcc.profile is not intended for user configuration as per the nvcc manual, but it was the solution which worked best for me.

Ewerton
  • 141
  • 1
  • 3
  • I suggest doing this, but pointing the path to a directory with g++ symlinked to the correct gcc version (especially useful if your distribution provides a supported gcc version). For example: `mkdir /usr/local/bin/cuda-hack && ln -s /usr/bin/g++-5 /usr/local/bin/cuda-hack` – Sami Liedes Jun 09 '17 at 21:00
2

CUDA is after some header modifications compatible with gcc4.7 and maybe higher version: https://www.udacity.com/wiki/cs344/troubleshoot_gcc47

42n4
  • 1,292
  • 22
  • 26
2

For people like me who get confused while using cmake, the FindCUDA.cmake script overrides some of the stuff from nvcc.profile. You can specify the nvcc host compiler by setting CUDA_HOST_COMPILER as per http://public.kitware.com/Bug/view.php?id=13674.

cheshirekow
  • 4,797
  • 6
  • 43
  • 47
  • `cmake .. -DCMAKE_INSTALL_PREFIX=/InstallPos_GPU/ -DCMAKE_C_COMPILER="/gcc-8.3.0/bin/gcc" -DCMAKE_CXX_COMPILER="/gcc-8.3.0/bin/g++" -DGMX_GPU=ON -DCUDA_TOOLKIT_ROOT_DIR=/cuda-7.5/ -D NVCCFLAGS=" -ccbin /cuda-7.5/bin/" -DCUDA_HOST_COMPILER=/cuda-7.5/bin/gcc` I have successfully installed the gromacs with GPU supporting. – pengchy Apr 15 '19 at 08:24
2

I had to install the older versions of gcc, g++.

    sudo apt-get install gcc-4.4
    sudo apt-get install g++-4.4

Check that gcc-4.4 is in /usr/bin/, and same for g++ Then I could use the solution above:

    sudo ln -s /usr/bin/gcc-4.4 /opt/cuda/bin/gcc
    sudo ln -s /usr/bin/g++-4.4 /opt/cuda/bin/g++
travelingbones
  • 7,919
  • 6
  • 36
  • 43
2

In $CUDA_HOME/include/host_config.h, find lines like these (may slightly vary between different CUDA version):

//...
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 9)

#error -- unsupported GNU version! gcc versions later than 4.9 are not supported!

#endif [> __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 9) <]
//...

Remove or change them matching your condition.

Note this method is potentially unsafe and may break your build. For example, gcc 5 uses C++11 as default, however this is not the case for nvcc as of CUDA 7.5. A workaround is to add

--Xcompiler="--std=c++98" for CUDA<=6.5

or

--std=c++11 for CUDA>=7.0.

Kh40tiK
  • 2,276
  • 19
  • 29
2

If you encounter this error, please read the log file:

$ cat /var/log/cuda-installer.log 
[INFO]: Driver installation detected by command: apt list --installed | grep -e nvidia-driver-[0-9][0-9][0-9] -e nvidia-[0-9][0-9][0-9]
[INFO]: Cleaning up window
[INFO]: Complete
[INFO]: Checking compiler version...
[INFO]: gcc location: /usr/bin/gcc

[INFO]: gcc version: gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) 

[ERROR]: unsupported compiler version: 9.2.1. Use --override to override this check.

Just follow the suggestion in the log file:

sudo sh cuda_<version>_linux.run --override

Job done :)

I just installed CUDA 10.2 with gcc 9.2 on Kubuntu 19.10 using the --override option.

Vanessa Deagan
  • 417
  • 6
  • 15
1

For CUDA 6.5 (and apparently 7.0 and 7.5), I've created a version of the gcc 4.8.5 RPM package (under Fedora Core 30) that allows that version of gcc to be install alongside your system's current GCC.

You can find all of that information here.

ulatekh
  • 1,311
  • 1
  • 14
  • 19
0

To compile the CUDA 8.0 examples on Ubuntu 16.10, I did:

sudo apt-get install gcc-5 g++-5
cd /path/to/NVIDIA_CUDA-8.0_Samples
# Find the path to the library (this should be in NVIDIA's Makefiles)
LIBLOC=`find /usr/lib -name "libnvcuvid.so.*" | head -n1 | perl -pe 's[/usr/lib/(nvidia-\d+)/.*][$1]'`
# Substitute that path into the makefiles for the hard-coded, incorrect one
find . -name "*.mk" | xargs perl -pi -e "s/nvidia-\d+/$LIBLOC/g"
# Make using the supported compiler
HOST_COMPILER=g++-5 make

This has the advantage of not modifying the whole system or making symlinks to just the binaries (that could cause library linking problems.)

Eponymous
  • 6,143
  • 4
  • 43
  • 43
0

This solved my problem:

sudo rm /usr/local/cuda/bin/gcc
sudo rm /usr/local/cuda/bin/g++
sudo apt install gcc-4.4 g++-4.4
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++
Scott
  • 4,974
  • 6
  • 35
  • 62
0

In my case, I had CUDA already installed from the Ubuntu version and cmake would detect that one instead of the newly installed version using the NVidia SDK Manager.

I ran dpkg -l | grep cuda and could see both versions.

What I had to do is uninstall the old CUDA (version 9.1 in my case) and leave the new version alone (version 10.2). I used the purge command like so:

sudo apt-get purge libcudart9.1 nvidia-cuda-dev nvidia-cuda-doc \
                                nvidia-cuda-gdb nvidia-cuda-toolkit

Please verify that the package names match the version you want to remove from your installation.

I had to rerun cmake from a blank BUILD directory to redirect all the #include and libraries to the SDK version (since the old paths were baked in the existing build environment).

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
-1

This is happening because your current CUDA version doesn't support your current GCC version. You need to do the following:

  1. Find the supported GCC version (in my case 5 for CUDA 9)

    • CUDA 4.1: GCC 4.5
    • CUDA 5.0: GCC 4.6
    • CUDA 6.0: GCC 4.7
    • CUDA 7.0: GCC 4.8
    • CUDA 7.5: GCC 4.8
    • CUDA 8: GCC 5.3
    • CUDA 9: GCC 5.5
    • CUDA 9.2: GCC 7
    • CUDA 10.1: GCC 8
  2. Install the supported GCC version

    sudo apt-get install gcc-5
    sudo apt-get install g++-5
    
  3. Change the softlinks for GCC in the /usr/bin directory

    cd /usr/bin
    sudo rm gcc
    sudo rm g++
    sudo ln -s /usr/bin/gcc-5 gcc
    sudo ln -s /usr/bin/g++-5 g++
    
  4. Change the softlinks for GCC in the /usr/local/cuda-9.0/bin directory

    cd /usr/local/cuda-9.0/bin
    sudo rm gcc
    sudo rm g++
    sudo ln -s /usr/bin/gcc-5 gcc
    sudo ln -s /usr/bin/g++-5 g++
    
  5. Add -DCUDA_HOST_COMPILER=/usr/bin/gcc-5 to your setup.py file, used for compilation

    if torch.cuda.is_available() and CUDA_HOME is not None:
        extension = CUDAExtension
        sources += source_cuda
        define_macros += [("WITH_CUDA", None)]
        extra_compile_args["nvcc"] = [
            "-DCUDA_HAS_FP16=1",
            "-D__CUDA_NO_HALF_OPERATORS__",
            "-D__CUDA_NO_HALF_CONVERSIONS__",
            "-D__CUDA_NO_HALF2_OPERATORS__",
            "-DCUDA_HOST_COMPILER=/usr/bin/gcc-5"
        ]
    
  6. Remove the old build directory

    rm -rd build/
    
  7. Compile again by setting CUDAHOSTCXX=/usr/bin/gcc-5

    CUDAHOSTCXX=/usr/bin/gcc-5 python setup.py build develop
    

Note: If you still get the gcc: error trying to exec 'cc1plus': execvp: no such file or directory error after following these steps, try reinstalling the GCC like this and then compiling again:

sudo apt-get install --reinstall gcc-5
sudo apt-get install --reinstall g++-5

Credits: https://github.com/facebookresearch/maskrcnn-benchmark/issues/25#issuecomment-433382510

tsveti_iko
  • 6,834
  • 3
  • 47
  • 39