0

I'm trying to write a CMake file which needs cuda functionalities. Consulting this answer, I added this line to my CMakeLists.txt:

set(CMAKE_CUDA_COMPILER  /usr/local/cuda-9.2/bin/nvcc)

But when using cmake command it still complains:

yuqiong@yuqiong-G7-7588:/media/yuqiong/DATA/alexnet/src/cpp/train$ cmake .
CMake Error: Could not find cmake module file: CMakeDetermineCUDACompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CUDA_COMPILER_ENV_VAR
CMake Error: Could not find cmake module file: /media/yuqiong/DATA/alexnet/src/cpp/train/CMakeFiles/3.5.1/CMakeCUDACompiler.cmake
CMake Error: Could not find cmake module file: CMakeCUDAInformation.cmake
CMake Error: Could not find cmake module file: CMakeTestCUDACompiler.cmake
-- Configuring incomplete, errors occurred!
See also "/media/yuqiong/DATA/alexnet/src/cpp/train/CMakeFiles/CMakeOutput.log".

Which seems confusing, as I don't know where else to set the environment variable? Any idea why the set command does not help cmake find the nvcc compiler?

Just in case helpful, here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CUDA_COMPILER  /usr/local/cuda-9.2/bin/nvcc)

project(train LANGUAGES CXX CUDA)

set(CMAKE_CXX_STANDARD 14)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )


add_executable(train train.cu)
target_link_libraries( train ${OpenCV_LIBS} )
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
yuqli
  • 4,461
  • 8
  • 26
  • 46

2 Answers2

2

Actually, you need CMake 3.8 on Linux to use project(train LANGUAGES CUDA). Before, you need the old way with:

FindPackage(CUDA)

And manually add the libraries.

The failure is because there is no native support for CUDA in your CMake version, use the old method.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • I see, thanks! If it's not too much to ask, can you share a link how to add libraries in cmake? Also, do I still need to specify the compiler to `nvcc` after adding libraries? – yuqli Dec 19 '18 at 21:35
  • Have a look at https://cliutils.gitlab.io/modern-cmake/chapters/packages/CUDA.html which explains both ways. – Matthieu Brucher Dec 19 '18 at 21:40
1

For anyone stumbled upon this question, here is the final CMakeLists.txt file I've used:

cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CUDA_COMPILER  /usr/local/cuda-9.2/bin/nvcc)

project(train)
include(FindCUDA)

set(CMAKE_CXX_STANDARD 14)
find_package( OpenCV REQUIRED )
find_package(CUDA REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)

include_directories( ${OpenCV_INCLUDE_DIRS} )

cuda_add_executable(train train.cu)
target_link_libraries( train ${OpenCV_LIBS} )
yuqli
  • 4,461
  • 8
  • 26
  • 46