从cmake 3.9版本开始,cmake就原生支持了cuda c/c++。在这之前,是通过find_package(CUDA REQUIRED)来间接支持cuda c/c++的。这种写法不仅繁琐而且丑陋。所以这里主要介绍3.9版本之后引入的方法,cuda c/c++程序可以使用和普通的c/c++程序完全一样的操作,甚至两者可以混合使用,非常的方便,清晰明了。
测试cuda是否正常安装:nvcc --version
CUDA编译器找不到的问题:
CMake Error at CMakeLists.txt:4 (project):
No CMAKE_cuda_COMPILER could be found.
解决方法一,指定路径:
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
# 指定CUDA编译器
set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc")
project(CUDA_MAT_MUL LANGUAGES C CXX CUDA)
解决方法二,环境变量(ubuntu18):
# 查看环境变量
env
# 添加路径
export PATH="$PATH:/usr/local/cuda/bin"
正常情况下,运行cmake命令输出:
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- The CUDA compiler identification is NVIDIA 10.2.89
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
参考:
BrightXiaoHan/CMakeTutorial
cmake-examples
learning-cmake
cmake-demo