[Solved] CUDA fails to compile in visual studio and throws error msb3721 and nvcc fatal

Platform: VS2013+CUDA 8.0
Executing “Compile” will pass, but “Executing “Recompile” or “Clean” will report an error.

nvcc fatal : Could not set up the environment for Microsoft Visual Studio using ‘C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/…/…/…/VC/bin/amd64/vcvars64.bat’
error MSB3721: 命令““C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe” -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64” -I”C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include” -I”C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include” –keep-dir x64\Release -maxrregcount=0 –machine 64 –compile -DWIN32 -DNDEBUG -D_CONSOLE -D_LIB -D_UNICODE -DUNICODE -Xcompiler “/EHsc /W3 /nologo /O2 /FS /Zi /MD ” -clean”已退出,返回代码为 1。

Guess the reason may be the computer installed at the same time vs2017, vs2019, cuda 10.1 multiple versions caused by the previous only vs2013 and cuda 8.0 when there is no problem
Solution.
Find vcvars64.bat under C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64 and modify this sentence inside.

@if not “%WindowsSDK_ExecutablePath_x64%” == “” (
@set “PATH=%WindowsSDK_ExecutablePath_x64%;%PATH%”
)

to

@if not “%WindowsSDK_ExecutablePath_x64%” == “” (
@set “PATH=!WindowsSDK_ExecutablePath_x64!;!PATH!”
)

Read More: