FindCUDAToolkit module 文件
/usr/share/cmake-3.25/Modules/FindCUDAToolkit.cmake
find FindCUDAToolkit
通过 CMake 的 FindCUDAToolkit
module 文件(/usr/share/cmake-3.25/Modules/FindCUDAToolkit.cmake
) 中的说明文档,可以查到这个文档输出的变量列表如下:
Result variables ^^^^^^^^^^^^^^^^
CUDAToolkit_FOUND
A boolean specifying whether or not the CUDA Toolkit was found.
CUDAToolkit_VERSION
The exact version of the CUDA Toolkit found (as reported bynvcc --version
orversion.txt
).
CUDAToolkit_VERSION_MAJOR
The major version of the CUDA Toolkit.
CUDAToolkit_VERSION_MINOR
The minor version of the CUDA Toolkit.
CUDAToolkit_VERSION_PATCH
The patch version of the CUDA Toolkit.
CUDAToolkit_BIN_DIR
The path to the CUDA Toolkit library directory that contains the CUDA executablenvcc
.
CUDAToolkit_INCLUDE_DIRS
The path to the CUDA Toolkitinclude
folder containing the header files required to compile a project linking against CUDA.
CUDAToolkit_LIBRARY_DIR
The path to the CUDA Toolkit library directory that contains the CUDA Runtime librarycudart
.CUDAToolkit_LIBRARY_ROOT
.. versionadded:: 3.18The path to the CUDA Toolkit directory containing the nvvm directory and version.txt.
CUDAToolkit_TARGET_DIR
The path to the CUDA Toolkit directory including the target architecture when cross-compiling. When not cross-compiling this will be equivalent to the parent directory ofCUDAToolkit_BIN_DIR
.
CUDAToolkit_NVCC_EXECUTABLE
The path to the NVIDIA CUDA compilernvcc
. Note that this path may not be the same as :variable:CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>
.nvcc
must be found to determine the CUDA Toolkit version as well as determining other features of the Toolkit. This variable is set for the convenience of modules that depend on this one.
使用 FOREACH
命令把这些变量都输出看一下:
FIND_PACKAGE (CUDAToolkit)
FOREACH (loop_var IN LISTS CUDAToolkit_FOUND CUDAToolkit_VERSION CUDAToolkit_VERSION_MAJOR CUDAToolkit_VERSION_MINOR CUDAToolkit_VERSION_PATCH CUDAToolkit_BIN_DIR CUDAToolkit_INCLUDE_DIRS CUDAToolkit_LIBRARY_DIR CUDAToolkit_LIBRARY_ROOT CUDAToolkit_TARGET_DIR CUDAToolkit_NVCC_EXECUTABLE)
MESSAGE (STATUS loop_var "=" ${loop_var})
ENDFOREACH()
运行结果
cmake .
-- loop_var=TRUE
-- loop_var=12.1.66
-- loop_var=12
-- loop_var=1
-- loop_var=66
-- loop_var=/usr/local/cuda/bin
-- loop_var=/usr/local/cuda/include
-- loop_var=/usr/local/cuda/lib64
-- loop_var=/usr/local/cuda
-- loop_var=/usr/local/cuda
-- loop_var=/usr/local/cuda/bin/nvcc
使用头文件和库
PROJECT (device_info LANGUAGES CXX CUDA)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
FIND_PACKAGE (CUDAToolkit)
INCLUDE_DIRECTORIES (${CUDAToolkit_INCLUDE_DIRS})
LINK_DIRECTORIES(${CUDAToolkit_LIBRARY_DIR})
ADD_EXECUTABLE (main main.cc)
TARGET_LINK_LIBRARIES (main cudart)