为什么VS2019Pro在通过CMake创建CUDA项目时会出现xutility、xmemory和atomic编译错误?
我正在尝试通过 CMake 创建一个简单的 CUDA 项目并遇到奇怪的编译错误。我正在关注本教程。最初,我使用的是 Visual Studio 2019 Community、CMake 3.18.3 和 CUDA 11.3,一切正常。然后,我更新到 Visual Studio 2019 Professional 和 CMake 3.20.3,但无法编译完全相同的源代码。
这是我的整个 CMakeLists 文件:
cmake_minimum_required(VERSION 3.18.3)
project(hello_world LANGUAGES CXX CUDA)
add_executable(hello_world_target main.cu)
target_compile_features(hello_world_target PUBLIC cxx_std_11)
set_target_properties(hello_world_target PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(hello_world_target PROPERTIES CUDA_ARCHITECTURES "52")
这是我唯一的源文件 main.cu:
#include <iostream>
int main(){
std::cout << "Hello, world!" << std::endl;
return 0;
}
当我尝试编译时,出现以下错误:
1>Compiling CUDA source file ..main.cu...
1>
1>C:Users[username]Documentshello_cmakebuild>"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.3binnvcc.exe" -gencode=arch=compute_52,code="compute_52,compute_52" -gencode=arch=compute_52,code="sm_52,compute_52" --use-local-env -ccbin "C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037binHostX64x64" -x cu -rdc=true -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.3include" --keep-dir x64Debug -maxrregcount=0 --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g -D_WINDOWS -D"CMAKE_INTDIR="Debug"" -D"CMAKE_INTDIR="Debug"" -D_MBCS -Xcompiler "/EHsc /W1 /nologo /Od /Fdhello_world_target.dirDebugvc142.pdb /FS /Zi /RTC1 /MDd /GR" -o hello_world_target.dirDebugmain.obj "C:Users[username]Documentshello_cmakemain.cu"
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexutility(1309): error : expected a "("
1> detected during instantiation of "void std::_Adl_verify_range(const _Iter &, const _Sentinel &) [with _Iter=const char *, _Sentinel=const char *]"
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexlocale(1990): here
1>
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexutility(1309): error : expected a "("
1> detected during instantiation of "void std::_Adl_verify_range(const _Iter &, const _Sentinel &) [with _Iter=__wchar_t *, _Sentinel=__wchar_t *]"
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexlocale(1991): here
1>
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexutility(1309): error : expected a "("
1> detected during instantiation of "void std::_Adl_verify_range(const _Iter &, const _Sentinel &) [with _Iter=const __wchar_t *, _Sentinel=const __wchar_t *]"
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includexlocale(2026): here
.....etc., etc., etc.....
31 errors detected in the compilation of "C:/Users/[username]/Documents/hello_cmake/main.cu".
1>main.cu
1>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalMSBuildMicrosoftVCv160BuildCustomizationsCUDA 11.3.targets(785,9): error MSB3721: The command ""C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.3binnvcc.exe" -gencode=arch=compute_52,code="compute_52,compute_52" -gencode=arch=compute_52,code="sm_52,compute_52" --use-local-env -ccbin "C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037binHostX64x64" -x cu -rdc=true -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.3include" --keep-dir x64Debug -maxrregcount=0 --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g -D_WINDOWS -D"CMAKE_INTDIR="Debug"" -D"CMAKE_INTDIR="Debug"" -D_MBCS -Xcompiler "/EHsc /W1 /nologo /Od /Fdhello_world_target.dirDebugvc142.pdb /FS /Zi /RTC1 /MDd /GR" -o hello_world_target.dirDebugmain.obj "C:Users[username]Documentshello_cmakemain.cu"" exited with code 1.
1>Done building project "hello_world_target.vcxproj" -- FAILED.
令人困惑的是,这适用于不同版本的 Visual Studio 和 CMake。此外,如果我重写 CMakeLists.txt 文件以CUDA从LANGUAGES列表中删除并更改main.cu为main.cpp,则一切正常。
编译器会抱怨 xutility、xmemory 和 atomic 文件也令人困惑。不过,这听起来像是一条红鲱鱼。
什么可能导致这个问题?
回答
从 Visual Studio 2019 16.9.6 升级到 16.10 后,我遇到了基本相同的问题。该问题似乎是由 Visual Studio 2019 16.10 随附的 MSVC v142 构建工具 14.29.30037 版本中 xutility、xmemory 等的更改引起的。
我无法解决新版本构建工具的问题,但我找到了一个解决方法。可以使用 VS2019 16.10 从 VS2019 16.9 安装 v142 构建工具:
在VS安装程序中,“Visual Studio 2019->Modify->Individual components”添加
- MSVC v142 - VS2019 C++ x64/x86 构建工具 (14.28-16.9)
- 适用于 v142 构建工具的 C++ v14.28 (16.9) ATL(x86 和 x64)
- 可选:MFC、命令行工具等。
要编译 CUDA CMake 项目,需要明确设置 MSVC 工具集版本。这可以通过输入来完成
version=14.28.29910
在“CMake GUI 中要使用的可选工具集(-T 的参数)中(需要删除 CMake 缓存)。