Could not set up environment (vcvars64.bat)

I keep getting the following error when I try to compile with nvcc from a Windows command line:

nvcc fatal: Could not set up environment for Microsoft Visual Studio using ‘E:/VisualStudio/VC/bin//…/…/VC/bin/amd64/vcvars64.bat’

I understand that a similar problem has been brought up in this forum many times in the past…the difference being that this time, that file definitely does exist. I expect that perhaps it is because the compiler is using ‘/’ instead of '' and the command line compiler is being futzy about it? Is there a way to set this, or am I possibly missing an obvious nvcc command-line option to fix this?

Thanks

You might want to give a little more context about what you are doing.

  1. Using Visual Studio (rather than command line) can you build and run CUDA projects correctly?
  2. What is the exact compile command line you are using?

The path used by nvcc looks like it is composited from multiple fragments, possibly using the VCINSTALLDIR or VSINSTALLDIR environment variables. On my Windows 7 system, I have this in my environment:

VSINSTALLDIR=c:\Program Files (x86)\Microsoft Visual Studio 10.0
VCINSTALLDIR=c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC
PATH=[…];c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64;[…]

I wonder whether your system may be missing one of these environment variables, or have inconsistent settings if they do exist. I also see a double slash “//” in your path. Not sure whether this could be a problem, but it could be an indication of incorrect compositing of the path or that one of the path components has a trailing slash when it shouldn’t have.

Hello

I’m having the exact same problem - CUDA 7.5, Visual Studio 2013, Windows 10.

The error message states:
nvcc fatal : Could not set up the environment for Microsoft Visual Studio using ‘C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/…/…/VC/bin/amd64/vcvars64.bat’

There are no double slashes or anything, the file is of course in that directory. I read other threads that metioned the missing .bat files, which I added as a precaution - that didn’t help either.

Could a developer go ahead and check what specific environment variable does nvcc look at when trying to run this .bat file, and what exactly is the problem there?

There’s a lot of people reporting this issue, so I think that fixing it would really make that first CUDA experience much more pleasant, and a lot of people - myself included - would be very happy to see it in a patch.

Thank you very much,

I just encountered this issue on Win 10 w/ the 8.0 toolkit.

I did a little investigating into the specific Batch file that was failing, and I was running into an error described and solved here.

https://support.microsoft.com/en-us/kb/2524009

I hope that helps someone, it appears the default batch file in my install runs into errors with nested parenthesis caused by variable expansion. I fixed it with a couple “!” in the first “if” statement of my batch file.

Hope this helps someone.

Could you please show you vcvars64.bat?

Here is the one line I changed.

@if not "%WindowsSDK_ExecutablePath_x64%" == "" (
	@set "PATH=!WindowsSDK_ExecutablePath_x64!;!PATH!"

)

And the file as a whole.

@call :GetVSCommonToolsDir
@if "%VS120COMNTOOLS%"=="" goto error_no_VS120COMNTOOLSDIR

@call "%VS120COMNTOOLS%VCVarsQueryRegistry.bat" No32bit 64bit

@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR
@if "%FrameworkDir64%"=="" goto error_no_FrameworkDIR64
@if "%FrameworkVersion64%"=="" goto error_no_FrameworkVer64
@if "%Framework40Version%"=="" goto error_no_Framework40Version

@set FrameworkDir=%FrameworkDir64%
@set FrameworkVersion=%FrameworkVersion64%

@if not "%WindowsSDK_ExecutablePath_x64%" == "" (
	@set "PATH=!WindowsSDK_ExecutablePath_x64!;!PATH!"

)

@if not "%WindowsSdkDir%" == "" (
	@set "PATH=%WindowsSdkDir%bin\x64;%WindowsSdkDir%bin\x86;%PATH%"
	@set "INCLUDE=%WindowsSdkDir%include\shared;%WindowsSdkDir%include\um;%WindowsSdkDir%include\winrt;%INCLUDE%"
	@set "LIB=%WindowsSdkDir%lib\winv6.3\um\x64;%LIB%"
	@set "LIBPATH=%WindowsSdkDir%References\CommonConfiguration\Neutral;%ExtensionSDKDir%\Microsoft.VCLibs\12.0\References\CommonConfiguration\neutral;%LIBPATH%"
)

@rem PATH
@rem ----
@if exist "%VSINSTALLDIR%Team Tools\Performance Tools\x64" (
	@set "PATH=%VSINSTALLDIR%Team Tools\Performance Tools\x64;%VSINSTALLDIR%Team Tools\Performance Tools;%PATH%"
)
@if exist "%ProgramFiles%\HTML Help Workshop" set PATH=%ProgramFiles%\HTML Help Workshop;%PATH%
@if exist "%ProgramFiles(x86)%\HTML Help Workshop" set PATH=%ProgramFiles(x86)%\HTML Help Workshop;%PATH%
@if exist "%VSINSTALLDIR%Common7\Tools" set PATH=%VSINSTALLDIR%Common7\Tools;%PATH%
@if exist "%VSINSTALLDIR%Common7\IDE" set PATH=%VSINSTALLDIR%Common7\IDE;%PATH%
@if exist "%VCINSTALLDIR%VCPackages" set PATH=%VCINSTALLDIR%VCPackages;%PATH%
@if exist "%FrameworkDir%\%Framework40Version%" set PATH=%FrameworkDir%\%Framework40Version%;%PATH%
@if exist "%FrameworkDir%\%FrameworkVersion%" set PATH=%FrameworkDir%\%FrameworkVersion%;%PATH%
@if exist "%VCINSTALLDIR%BIN\amd64" set PATH=%VCINSTALLDIR%BIN\amd64;%PATH%

@rem Add path to MSBuild Binaries
@if exist "%ProgramFiles%\MSBuild\12.0\bin\amd64" set PATH=%ProgramFiles%\MSBuild\12.0\bin\amd64;%PATH%
@if exist "%ProgramFiles(x86)%\MSBuild\12.0\bin\amd64" set PATH=%ProgramFiles(x86)%\MSBuild\12.0\bin\amd64;%PATH%

@if exist "%VSINSTALLDIR%Common7\IDE\CommonExtensions\Microsoft\TestWindow" (
	@set "PATH=%VSINSTALLDIR%Common7\IDE\CommonExtensions\Microsoft\TestWindow;%PATH%"
)

@rem INCLUDE
@rem -------
@if exist "%VCINSTALLDIR%ATLMFC\INCLUDE" set INCLUDE=%VCINSTALLDIR%ATLMFC\INCLUDE;%INCLUDE%
@if exist "%VCINSTALLDIR%INCLUDE" set INCLUDE=%VCINSTALLDIR%INCLUDE;%INCLUDE%

@rem LIB
@rem ---
@if exist "%VCINSTALLDIR%ATLMFC\LIB\amd64" set LIB=%VCINSTALLDIR%ATLMFC\LIB\amd64;%LIB%
@if exist "%VCINSTALLDIR%LIB\amd64" set LIB=%VCINSTALLDIR%LIB\amd64;%LIB%

@rem LIBPATH
@rem -------
@if exist "%VCINSTALLDIR%ATLMFC\LIB\amd64" set LIBPATH=%VCINSTALLDIR%ATLMFC\LIB\amd64;%LIBPATH%
@if exist "%VCINSTALLDIR%LIB\amd64" set LIBPATH=%VCINSTALLDIR%LIB\amd64;%LIBPATH%
@if exist "%FrameworkDir%\%Framework40Version%" set LIBPATH=%FrameworkDir%\%Framework40Version%;%LIBPATH%
@if exist "%FrameworkDir%\%FrameworkVersion%" set LIBPATH=%FrameworkDir%\%FrameworkVersion%;%LIBPATH%

@set Platform=X64
@set CommandPromptType=Native

@goto end

@REM -----------------------------------------------------------------------
:GetVSCommonToolsDir
@set VS120COMNTOOLS=
@call :GetVSCommonToolsDirHelper32 HKLM > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper32 HKCU > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKLM > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKCU > nul 2>&1
@exit /B 0

:GetVSCommonToolsDirHelper32
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v "12.0"') DO (
	@if "%%i"=="12.0" (
		@SET "VS120COMNTOOLS=%%k"
	)
)
@if "%VS120COMNTOOLS%"=="" exit /B 1
@SET "VS120COMNTOOLS=%VS120COMNTOOLS%Common7\Tools\"
@exit /B 0

:GetVSCommonToolsDirHelper64
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "12.0"') DO (
	@if "%%i"=="12.0" (
		@SET "VS120COMNTOOLS=%%k"
	)
)
@if "%VS120COMNTOOLS%"=="" exit /B 1
@SET "VS120COMNTOOLS=%VS120COMNTOOLS%Common7\Tools\"
@exit /B 0

@REM -----------------------------------------------------------------------
:error_no_VS120COMNTOOLSDIR
@echo ERROR: Cannot determine the location of the VS Common Tools folder.
@goto end

:error_no_VSINSTALLDIR
@echo ERROR: Cannot determine the location of the VS installation.
@goto end

:error_no_VCINSTALLDIR
@echo ERROR: Cannot determine the location of the VC installation.
@goto end

:error_no_FrameworkDIR64
@echo ERROR: Cannot determine the location of the .NET Framework 64bit installation.
@goto end

:error_no_FrameworkVer64
@echo ERROR: Cannot determine the version of the .NET Framework 64bit installation.
@goto end

:error_no_Framework40Version
@echo ERROR: Cannot determine the .NET Framework 4.0 version.
@goto end

:end

Oh, thank you very much for the prompt reply!

I guess it is the file from VS2013 distribution. But unfortunately I’m using VS2015. I haven’t found any troubles with parentheses in my file and I still have the problem described by the topic starter. :(

Yes, that was the file from the VS2013 distribution.

I figured my solution out by running the bat file inside a command prompt by itself. It gave a few errors for me when I looked closely. You might try that with VS2015’s bat file and see if you can come upon a problem.

[edit]
I also have VS2015 installed, I ran my vcvars64.bat, no output was given (meaning it ran successfully) you may have a different experience though.

I’m having the same problem too.
— CUDA 8.0, Visual Studio 2013, Windows 7.

My run_windows.bat commond as follow:

call “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat” amd64

CC = “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\cl.exe”

CUDA_FLAGS1 = -gencode=arch=compute_30,code="sm_30,compute_30" -ccbin $(CC) -x cu $(CUDA_INCLUDE) -G -maxrregcount=0 --machine 64 -cudart static -ptx

When I running with bat file, I get the 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/amd64/…/…/…/VC/bin/amd64/vcvars64.bat’

Finally, I added " --use-local-env --cl-version 2013" to CUDA_FLAGS1 variable, it’s work successfully.
It like this:

CUDA_FLAGS1 = -gencode=arch=compute_30,code="sm_30,compute_30" -ccbin $(CC) -x cu $(CUDA_INCLUDE) --use-local-env --cl-version 2013
-G -maxrregcount=0 --machine 64 -cudart static -ptx

Same problem for me. I am working on Windows 10 Pro and I have installed Microsoft Visual Studio 2017 Enterprise and Cuda 9.2. I am trying to compile a .cu file with cmd because the integration of Cuda in Visual Studio didn’t worked. Even when I reinstalled my grafic driver and cuda like it was described here:

It is not working.
So I am trying it with the cmd command now.

When It type in cmd: nvcc hello.cu
I always get the message:

nvcc fatal : Could not set up the environment for Microsoft Visual Studio using ‘C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/…/…/VC/bin/amd64/vcvars64.bat’

I tryed already the solution from TylerHartwig with the ! but this had no effect. But I am not sure if I did this right. I was doing these changes in the vcvars64.bat file

What can I do. I am a absolutely newby, so please be patient

thank you very much in advance

Marcel

After some digging around I found that I get this error if there are any directories in the PATH,INCLUDE,LIB,LIBPATH env variables that have double quotes around them. Make sure that there are no quotes in any of these environment variables.