Search code examples
buildcudacmakenvcc

CMake 3.x + CUDA - compilation busted


I've written the following groundbreaking GPU-powered application:

int main() { return 0; }

and I'm trying to build it using CMake. Here's my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -std=c++11" )
find_package(CUDA QUIET REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} " -std=c++11")
cuda_add_executable(a a.cu)

With this file, and for some reason, the compilation fails; it looks like (although I'm not sure) the reason is that CMake is having NVCC use the CUDA include directory twice. The result:

/home/joeuser/opt/cuda/bin/nvcc -M -D__CUDACC__ /home/joeuser/src/kt3/a.cu -o /home/joeuser/src/kt3/CMakeFiles/a.dir//a_generated_a.cu.o.NVCC-depend -ccbin /usr/bin/ccache -m64 --std c++11 -Xcompiler ,\"-g\" -std=c++11 -DNVCC -I/home/joeuser/opt/cuda/include -I/home/joeuser/opt/cuda/include
nvcc fatal   : redefinition of argument 'std'
CMake Error at a_generated_a.cu.o.cmake:207 (message):
  Error generating
  /home/joeuser/src/kt3/CMakeFiles/a.dir//./a_generated_a.cu.o

You might be asking why my MWE is not more terse. Why do I need those option-setting lines above if I don't use C++11 anyway? Well, if I remove them, I still get the double-include, but have an earlier failure involving ccache about which I will probably ask another question.

So is the problem actually the double-include, or is it something else? And what should I do?

Additional information:

  • I don't have root on this machine.
  • CMake version: 3.3.2 .
  • Distribution: Fedora 22 (sorry, I can't help it; but I also seem to be getting this on Debian Stretch as well).
  • CUDA install location: $HOME/opt/cuda , and its binary directory is in $PATH.
  • On another system, with a different configuration and distro (Fedora 20, CUDA 7.5 but in another local dir, possibly other differences) I do not get this behavior.

Solution

  • I think the problem is that the nvcc flags are propagated to your c/c++ compiler, so some compiler arguments are effectively passed twice. Try using SET(CUDA_PROPAGATE_HOST_FLAGS OFF)