title: Caffe compilation fails due to unsupported gcc compiler version
body: <p>I struggle with <a href="http://caffe.berkeleyvision.org/" rel="nofollow noreferrer">Caffe</a> compilation. Unfortunately I failed to compile it.</p>
  
  <p><a href="http://caffe.berkeleyvision.org/installation.html#cmake-compilation" rel="nofollow noreferrer">Steps</a> I followed:</p>
  
  <pre class="lang-none prettyprint-override"><code>git clone https://github.com/BVLC/caffe.git
  cd caffe
  mkdir build
  cd build
  cmake ..
  make all
  </code></pre>
  
  <p>Running <code>make all</code> fails with the following error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>[  2%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile.dir/util/cuda_compile_generated_im2col.cu.o
  In file included from /usr/include/cuda_runtime.h:59:0,
                   from &lt;command-line&gt;:0:
  /usr/include/host_config.h:82:2: error: #error -- unsupported GNU version! gcc 4.9 and up are not supported!
   #error -- unsupported GNU version! gcc 4.9 and up are not supported!
    ^
  CMake Error at cuda_compile_generated_im2col.cu.o.cmake:207 (message):
    Error generating /mydir/caffe/build/src/caffe/CMakeFiles/cuda_compile.dir/util/./cuda_compile_generated_im2col.cu.o
  </code></pre>
  
  <p>Software version:</p>
  
  <ul>
  <li>OS: <code>Debian</code>.</li>
  <li><code>gcc</code> version: <code>5.3.1</code>.</li>
  <li><code>nvcc</code> version: <code>6.5.12</code>.</li>
  <li><code>cat /proc/driver/nvidia/version</code> result:</li>
  </ul>
  
  <pre class="lang-none prettyprint-override"><code>NVRM version: NVIDIA UNIX x86_64 Kernel Module  352.63  Sat Nov  7 21:25:42 PST 2015
  GCC version:  gcc version 4.8.5 (Debian 4.8.5-3) 
  </code></pre>
  
  <hr>
  
  <h2>Attempts to solve the problem</h2>
  
  <h3><em>1st try</em></h3>
  
  <p>Simple solutions are often best ones, so (as suggested <a href="https://devtalk.nvidia.com/default/topic/516939/cuda4-1-gcc-4-6/" rel="nofollow noreferrer">here</a>) I tried to comment out macro checking <code>gcc</code> version from <code>/usr/include/host_config.h</code> (line 82). Unfortunately it doesn't work and compilation fails badly:</p>
  
  <pre class="lang-none prettyprint-override"><code>1 catastrophic error detected in the compilation of "/tmp/tmpxft_000069c2_00000000-4_im2col.cpp4.ii".
  </code></pre>
  
  <h3><em>2nd try</em></h3>
  
  <p>I tried to run:</p>
  
  <pre class="lang-none prettyprint-override"><code>cmake -D CMAKE_CXX_COMPILER=g++-4.8 ..
  make
  </code></pre>
  
  <p>but it fails with <em>exactly</em> the same error message (even though <code>g++-4.8</code> should be accepted).</p>
  
  <h3><em>3rd try</em></h3>
  
  <p>I've found <a href="https://stackoverflow.com/questions/28009785/cuda-6-5-complains-about-not-supporting-gcc-4-9-what-to-do">similar problem</a> (though not related to <em>Caffe</em>) and I tried to solve it as suggested in the accepted answer.</p>
  
  <p>What I did:</p>
  
  <ol>
  <li>I've ran <code>grep -iR "find_package(CUDA" caffe</code> command and found <a href="https://github.com/BVLC/caffe/blob/master/cmake/Cuda.cmake" rel="nofollow noreferrer"><code>Cuda.cmake</code></a> file which has <code>find_package(CUDA 5.5 QUIET)</code> in line <a href="https://github.com/BVLC/caffe/blob/master/cmake/Cuda.cmake#L225" rel="nofollow noreferrer">225</a>.</li>
  <li>I added <code>set(CUDA_HOST_COMPILER /usr/bin/gcc-4.8)</code> to <a href="https://github.com/BVLC/caffe/blob/master/cmake/Cuda.cmake" rel="nofollow noreferrer"><code>Cuda.cmake</code></a>, line before line: <code>find_package(CUDA 5.5 QUIET)</code>.</li>
  <li>I removed everything from <code>build</code> directory and ran <code>cmake</code> and <code>make</code> again - with and without <code>-D CMAKE_CXX_COMPILER=g++-4.8</code>.</li>
  </ol>
  
  <p>Unfortunately result is exactly the same. Caffe probably overwrites it somehow - I didn't figure it out <em>how</em>.</p>
  
  <p><code>make VERBOSE=1 2&gt;&amp;1 | grep -i compiler-bindir</code> returns nothing.</p>
  
  <p>What's interesting, <code>make VERBOSE=1</code> prints command that fails, which is:</p>
  
  <pre class="lang-none prettyprint-override"><code>/usr/bin/nvcc -M -D__CUDACC__ /mydir/caffe/src/caffe/util/im2col.cu -o /mydir/caffe/build/src/caffe/CMakeFiles/cuda_compile.dir/util/cuda_compile_generated_im2col.cu.o.NVCC-depend -ccbin /usr/bin/cc -m64 -DUSE_LMDB -DUSE_LEVELDB -DUSE_OPENCV -DWITH_PYTHON_LAYER -DGTEST_USE_OWN_TR1_TUPLE -Xcompiler ,\"-fPIC\",\"-Wall\",\"-Wno-sign-compare\",\"-Wno-uninitialized\",\"-O3\",\"-DNDEBUG\" -gencode arch=compute_20,code=sm_21 -Xcudafe --diag_suppress=cc_clobber_ignored -Xcudafe --diag_suppress=integer_sign_change -Xcudafe --diag_suppress=useless_using_declaration -Xcudafe --diag_suppress=set_but_not_used -Xcompiler -fPIC -DNVCC -I/usr/include -I/mydir/caffe/src -I/usr/include -I/mydir/caffe/build/include -I/usr/include/hdf5/serial -I/usr/include/opencv -I/usr/include/atlas -I/usr/include/python2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/mydir/caffe/include -I/mydir/caffe/build
  </code></pre>
  
  <p>when I add <code>--compiler-bindir /usr/bin/gcc-4.8</code> flag manually, it prints error:</p>
  
  <pre class="lang-none prettyprint-override"><code>nvcc fatal   : redefinition of argument 'compiler-bindir'
  </code></pre>
  
  <p>which may be related to <a href="https://cmake.org/Bug/view.php?id=13674" rel="nofollow noreferrer">this</a> bug report.</p>
  
  <p><strong>Edit:</strong> I didn't notice that <code>--compiler-bindir</code> and <code>-ccbin</code> are the same options, and the latter is already set in above command that failed. When I changed <code>-ccbin /usr/bin/cc</code> to <code>-ccbin /usr/bin/gcc-4.8</code> in above command that failed, it completes successfully. Now I need to find option in Caffe's CMake file that overwrite <code>-ccbin</code> in all subsequent Caffe's CMakes. Looking at <code>cmake/Cuda.cmake:252:list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_EXTRA}</code> seems to be good way to go.</p>
  
  <hr>
  
  <p>How can I successfully complete my compilation? Any help is appreciated.</p>
  
  <p>Related SO questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/33886654/host-config-hunsupported-gnu-version-gcc-versions-later-than-4-9-are-not-suppo"><em>host_config.h:unsupported GNU version! gcc versions later than 4.9 are not supported</em>.</a></li>
  <li><a href="https://stackoverflow.com/questions/28009785/cuda-6-5-complains-about-not-supporting-gcc-4-9-what-to-do"><em>CUDA 6.5 complains about not supporting gcc 4.9 - what to do?</em>.</a></li>
  </ul>