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_FOUNDA boolean specifying whether or not the CUDA Toolkit was found.
CUDAToolkit_VERSIONThe exact version of the CUDA Toolkit found (as reported bynvcc --versionorversion.txt).
CUDAToolkit_VERSION_MAJORThe major version of the CUDA Toolkit.
CUDAToolkit_VERSION_MINORThe minor version of the CUDA Toolkit.
CUDAToolkit_VERSION_PATCHThe patch version of the CUDA Toolkit.
CUDAToolkit_BIN_DIRThe path to the CUDA Toolkit library directory that contains the CUDA executablenvcc.
CUDAToolkit_INCLUDE_DIRSThe path to the CUDA Toolkitincludefolder containing the header files required to compile a project linking against CUDA.
CUDAToolkit_LIBRARY_DIRThe 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_DIRThe 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_EXECUTABLEThe path to the NVIDIA CUDA compilernvcc. Note that this path may not be the same as :variable:CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>.nvccmust 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)