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



我正在开发一个使用CUDA的小型库,但当我从命令行运行它时,我似乎无法使CMake工作,即使我的IDE(CLion(使用CMake成功地调试了程序。

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

CLion能够完美地编译代码,一切都按预期进行,但当我从命令行运行CMake时,它会出现以下错误:

PS C:UserspenciOneDriveDesktopRapidTemporaryRapidbuild> 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环境变量中,但它需要位于Cygwin64bin目录的之上。这意味着从命令行运行cmake命令将引用Visual Studio安装,而不是Cygwin64安装,从而使其能够正常工作。

希望这对未来的其他人也有帮助

最新更新