title: Permissions error with CMake and CUDA
body: <p>I'm trying to build a project with a static library using CUDA along with C++ code. It needs to link with an external library (moderngpu). The CUDA and C++ code each independently work fine, and have been previously compiled with make. However, when trying to build the code with CMake (through CLion, if it matters), I get the following error:  </p>
  
  <pre><code>Error running 'main': Cannot run program "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir/cmake_device_link.o" (in directory "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir"): error=13, Permission denied
  </code></pre>
  
  <p>I've been trying to solve this for days, but haven't been able to make any progress. It doesn't make sense that I'm getting a permissions error at all, and the <code>.o</code> file it's complaining about doesn't run when I set it to executable. My best guess is that it's some sort of linking issue. Any help would be appreciated.</p>
  
  <p><code>CMakeLists.txt</code></p>
  
  <pre><code>CMAKE_MINIMUM_REQUIRED(VERSION 3.9 FATAL_ERROR)
  PROJECT(myProject LANGUAGES CXX CUDA)
  
  FIND_PACKAGE(CUDA 8.0 REQUIRED)
  
  SET(CMAKE_CXX_STANDARD 17)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
  -Wall -Wextra -Wno-deprecated -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
  -Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual -Wpedantic \
  -ffast-math \
  ")
  SET(CMAKE_CUDA_COMPILER /usr/local/cuda-8.0/bin/nvcc)
  SET(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} \
      -Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC \
      -gencode arch=compute_61,code=sm_61 \
  ")
  
  ADD_SUBDIRECTORY(src)
  ADD_SUBDIRECTORY(lib)
  </code></pre>
  
  <p><code>src/CMakeLists.txt</code></p>
  
  <pre><code>INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
  INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
  
  AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)
  
  ADD_EXECUTABLE(stuff ${source})
  
  TARGET_LINK_LIBRARIES(stuff LINK_PUBLIC thing thingKernels)
  </code></pre>
  
  <p><code>lib/CMakeLists.txt</code></p>
  
  <pre><code>INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
  INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
  INCLUDE_DIRECTORIES(
      /usr/local/src/moderngpu/include
      /usr/local/cuda-8.0/include
  )
  
  ADD_LIBRARY(thing STATIC
      ${CMAKE_CURRENT_SOURCE_DIR}/thing.cpp
  )
  
  ADD_LIBRARY(thingKernels STATIC
      ../include/cuda/thing.cu
      /usr/local/src/moderngpu/src/mgpucontext.cu
      /usr/local/src/moderngpu/src/mgpuutil.cpp
  )
  
  TARGET_INCLUDE_DIRECTORIES(thing PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})
  TARGET_INCLUDE_DIRECTORIES(thingKernels PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})
  </code></pre>
  
  <p>For reference, this is the working <code>Makefile</code></p>
  
  <pre><code>ifndef CUDA_VERSION
      CUDA_VERSION = 8.0
  endif
  ifndef COMPUTE_CAPABILITY
      COMPUTE_CAPABILITY = 61
  endif
  
  CPP_COMPILER = g++
  CPP_FLAGS = -pthread -fno-strict-aliasing -g -O2 -g -fwrapv -O3 -Wall -fPIC
  
  CUDA_COMPILER = nvcc -m64
  CUDA_FLAGS = -Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC
  CUDA_FLAGS += -gencode arch=compute_$(COMPUTE_CAPABILITY),code=sm_$(COMPUTE_CAPABILITY)
  
  CUDA_INCLUDE = /usr/local/cuda-$(CUDA_VERSION)/include
  MGPU_INCLUDE = /usr/local/src/moderngpu/include
  INCLUDE_FLAGS = -I$(CUDA_INCLUDE) -I$(MGPU_INCLUDE)
  
  CUDA_LIB = /usr/local/cuda-$(CUDA_VERSION)/lib64
  LINK_FLAGS = -L$(CUDA_LIB)
  
  MGPU_SRC = /usr/local/src/moderngpu/src
  
  TARGETS = thing
  
  all: $(TARGETS)
  
  thing: thing.co | mgpuutil.o mgpucontext.o
      $(CPP_COMPILER) $(CPP_FLAGS) $&lt; $(LINK_FLAGS) -lcudart -o $@ $|
  
  %.co: %.cu
      $(CUDA_COMPILER) $(CUDA_FLAGS) $(INCLUDE_FLAGS) $(LINK_FLAGS) -o $@ -c $&lt;
  
  mgpucontext.o:  $(MGPU_SRC)/mgpucontext.cu
      $(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $&lt;
  
  mgpuutil.o:  $(MGPU_SRC)/mgpuutil.cpp
      $(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $&lt;
  
  clean:
      rm -f *.o *.co $(TARGETS)
  </code></pre>