CMake——CUDA 编译器无法编译简单的测试程序

CMake -- CUDA compiler is not able to compile a simple test program

我正在开发一个使用 CUDA 的小型库,但是当我从命令行 运行 时,我似乎无法使 CMake 工作,即使我的 IDE (CLion)使用CMake编译程序成功

我已经在互联网上搜索了很长时间,试图解决这个问题,但没有什么太大的不同。我有 CUDA 11.1 和 CMake 3.17.3,两者都可以从命令行访问。

CLion 能够完美地编译代码,一切都按预期工作,但是当我从命令行 运行 CMake 时,它​​给出了以下错误:

PS C:\Users\penci\OneDrive\Desktop\Rapid\Temporary\Rapid\build> cmake ..
-- The CUDA compiler identification is NVIDIA 11.1.105
-- Check for working CUDA compiler: /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe
-- Check for working CUDA compiler: /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe - broken
CMake Error at /usr/share/cmake-3.17.3/Modules/CMakeTestCUDACompiler.cmake:46 (message):
  The CUDA compiler

    "/cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp

    Run Build Command(s):/usr/bin/make.exe cmTC_2c864/fast && /usr/bin/make  -f CMakeFiles/cmTC_2c864.dir/build.make CMakeFiles/cmTC_2c864.dir/build
    make[1]: Entering directory '/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp'
    Building CUDA object CMakeFiles/cmTC_2c864.dir/main.cu.o
    "/cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe" -forward-unknown-to-host-compiler    -x cu -c /cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp/main.cu -o CMakeFiles/cmTC_2c864.dir/main.cu.o
    c1xx: fatal error C1083: Cannot open source file: 'C:/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp/main.cu': No such file or directory
    main.cu
    make[1]: *** [CMakeFiles/cmTC_2c864.dir/build.make:86: CMakeFiles/cmTC_2c864.dir/main.cu.o] Error 2
    make[1]: Leaving directory '/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp'
    make: *** [Makefile:141: cmTC_2c864/fast] Error 2





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeOutput.log".
See also "/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeError.log".

对不起,文字墙太多了,但我不确定为什么这在命令行中不起作用,而在 CLion 中却起作用。

这是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.17)
project(Rapid LANGUAGES CUDA)

set(CMAKE_CUDA_STANDARD 14)

include_directories(${PROJECT_SOURCE_DIR}/include/rapid/graphics/GLFW)
link_directories(${PROJECT_SOURCE_DIR}/include/rapid/graphics/GLFW/lib64)

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler /openmp")

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)

add_executable(Rapid main.cu)

set_target_properties(
        Rapid
        PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

target_link_libraries(Rapid cublas glfw3 gdi32 opengl32)

任何关于如何解决这个问题的想法都将不胜感激,因为我花了几天时间试图解决这个问题,但网上似乎没有任何效果。

在谷歌搜索一些似乎与该问题完全无关的内容后,我发现我的 CMake Cygwin64 安装未设置为使用 Visual Studio 生成器。

为了解决这个问题,我需要将 Visual Studio 安装的 CMake 放入 PATH 环境变量中,但它需要 之上Cygwin64 bin 目录。这意味着 运行 来自命令行的 cmake 命令将引用 Visual Studio 安装,而不是 Cygwin64 安装,从而使其正常工作。

希望这对以后的其他人也有帮助