解决在QT中编写CUDA程序出现nvcc fatal : Could not set up the environment for Microsoft Visual Studio using的问题
问题详情
在QT编写CUDA代码,在已经配好.pro文件中的代码,并且CUDA安装没有问题,还可以在VS2017中正常运行CUDA程序时,一开始debug的时候我遇到了以下问题: Could not set up the environment for Microsoft Visual Studio using,
nvcc fatal : Could not set up the environment for Microsoft Visual Studio using 'C:/Program Files (x86)
/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/../../../../../../../VC/Auxiliary/Build/vcvars64.bat'
我在网上找了好久的解决办法,国外的社区还有国内的社区,都没有详细可靠的解决方案,总结下来他们的方法大致包括检查系统的环境变量,CUDA的路径不能有空格,nvcc的输入太长等,但这些都不能解决这个问题。
这确实花费了我很长的时间还有精力,因此我发布出我的解决方案,后面也有详细的解决过程,希望我的分享可以帮助到你。
解决方案
下面我会给出解决方案,想直接解决问题的看完这一部分就行了,想了解我解决问题的过程的可以看第三部分。
下面是解决方案:找到报错中的文件路径,可以参考我的路径:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build
找到相应的文件,用记事本打开vcvarsall.bat,复制其中的代码到vcvars64.bat,最终vcvars64.bat中的内容为:
@if not "%VSCMD_DEBUG%" GEQ "3" echo off
@REM -------------------------------------------------------------------------
:parse_args
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_VER=
@REM Parse command line arguments. This implementation does not care about
@REM argument order.
if "%VSCMD_DEBUG%" GEQ "1" (
@echo [DEBUG:%~nx0] init with arguments '%*'
)
set __VCVARSALL_PARSE_ERROR=0
set "__VCVARSALL_ARGS_LIST=%*"
call :parse_loop
set __VCVARSALL_ARGS_LIST=
if "%VSCMD_DEBUG%" GEQ "1" (
@echo [DEBUG:%~nx0] Command line parse completed with values:
@echo [DEBUG:%~nx0] __VCVARSALL_TARGET_ARCH='%__VCVARSALL_TARGET_ARCH%'
@echo [DEBUG:%~nx0] __VCVARSALL_HOST_ARCH='%__VCVARSALL_HOST_ARCH%'
@echo [DEBUG:%~nx0] __VCVARSALL_WINSDK='%__VCVARSALL_WINSDK%'
@echo [DEBUG:%~nx0] __VCVARSALL_STORE='%__VCVARSALL_STORE%'
@echo [DEBUG:%~nx0] __VCVARSALL_HELP='%__VCVARSALL_HELP%'
@echo [DEBUG:%~nx0] __VCVARSALL_PARSE_ERROR='%__VCVARSALL_PARSE_ERROR%'
)
if "%__VCVARSALL_CLEAN_ENV%" NEQ "" goto :call_vsdevcmd
if "%__VCVARSALL_PARSE_ERROR%" NEQ "0" goto :usage_error
if "%__VCVARSALL_HELP%" NEQ "" goto :usage
@REM -------------------------------------------------------------------------
:check_platform
@REM This script is installed to ...\VC\Auxiliary\Build.
@REM vsdevcmd is installed to ...\Common7\Tools.
if not exist "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" goto missing
@REM Assemble the arguments to pass to vsdevcmd.bat
if "%__VCVARSALL_TARGET_ARCH%" == "" goto :usage_error
if "%__VCVARSALL_HOST_ARCH%" == "" goto :usage_error
set "__VCVARSALL_VSDEVCMD_ARGS=-arch=%__VCVARSALL_TARGET_ARCH% -host_arch=%__VCVARSALL_HOST_ARCH%"
if "%__VCVARSALL_WINSDK%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -winsdk=%__VCVARSALL_WINSDK%"
)
if "%__VCVARSALL_STORE%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -app_platform=UWP"
)
if "%__VCVARSALL_VER%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_ver=%__VCVARSALL_VER%"
)
if "%__VCVARSALL_SPECTRE%" NEQ "" (
set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_spectre_libs=%__VCVARSALL_SPECTRE%"
)
goto :call_vsdevcmd
@REM -------------------------------------------------------------------------
@REM Call vsdevcmd.bat to setup the command prompt environment
:call_vsdevcmd
@REM This temporary environment variable is used to control setting of VC++
@REM command prompt-specific environment variables that should not be set
@REM by the VS Developer Command prompt (specifically vsdevcmd\ext\vcvars.bat).
@REM The known case this effects is the Platform environment variable, which
@REM will override platform target for .NET builds.
set VSCMD_VCVARSALL_INIT=1
@REM Special handling for the /clean_env argument
if "%__VCVARSALL_CLEAN_ENV%" NEQ "" (
call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" /clean_env
goto :end
)
call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" %__VCVARSALL_VSDEVCMD_ARGS%
if "%ERRORLEVEL%"=="0" (
@REM Print the target command prompt architecture...
if "%__VCVARSALL_HOST_ARCH%" NEQ "%__VCVARSALL_TARGET_ARCH%" (
echo [%~nx0] Environment initialized for: '%__VCVARSALL_HOST_ARCH%_%__VCVARSALL_TARGET_ARCH%'
) else (
echo [%~nx0] Environment initialized for: '%__VCVARSALL_TARGET_ARCH%'
)
)
goto :end
:parse_loop
for /F "tokens=1,* delims= " %%a in ("%__VCVARSALL_ARGS_LIST%") do (
if "%VSCMD_DEBUG%" GEQ "2" (
@echo [DEBUG:%~nx0] inner argument {%%a}
)
call :parse_argument %%a
set "__VCVARSALL_ARGS_LIST=%%b"
goto :parse_loop
)
exit /B 0
:parse_argument
@REM called by :parse_loop and expects the arguments to either be:
@REM 1. a single argument in %1
@REM 2. an argument pair from the command line specified as '%1=%2'
set __local_ARG_FOUND=
@REM Architecture
if /I "%1"=="x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_amd64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_x64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x86
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64" (
set __VCVARSALL_TARGET_ARCH=x64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_x86" (
set __VCVARSALL_TARGET_ARCH=x86
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm" (
set __VCVARSALL_TARGET_ARCH=arm
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm64" (
set __VCVARSALL_TARGET_ARCH=arm64
set __VCVARSALL_HOST_ARCH=x64
set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_ver" (
set "__VCVARSALL_VER=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_ver" (
set "__VCVARSALL_VER=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_ver" (
set "__vcvarsall_ver=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_spectre_libs" (
set "__VCVARSALL_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_spectre_libs" (
set "__VCVARSALL_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_spectre_libs" (
set "__vcvarsall_SPECTRE=%2"
set __local_ARG_FOUND=1
)
if /I "%1"=="help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="/help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="-help" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="/?" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
if /I "%1"=="-?" (
set __VCVARSALL_HELP=1
set __local_ARG_FOUND=1
)
@REM -- /clean_env --
@REM Mostly used for internal testing to restore the state of
@REM the command line environment to its state prior to vcvarsall.bat
@REM being executed.
if /I "%1"=="/clean_env" (
set __VCVARSALL_CLEAN_ENV=/clean_env
set __local_ARG_FOUND=1
)
if /I "%1"=="-clean_env" (
set __VCVARSALL_CLEAN_ENV=/clean_env
set __local_ARG_FOUND=1
)
@REM Windows SDK Version
if /I "%1"=="8.1" (
set "__VCVARSALL_WINSDK=8.1"
set __local_ARG_FOUND=1
)
set __temp1=%1
if /I "%__temp1:~0,3%"=="10." (
set "__VCVARSALL_WINSDK=%1"
set __local_ARG_FOUND=1
)
set __temp1=
@REM Store/UWP
if /I "%1"=="store" (
set "__VCVARSALL_STORE=-app_platform=UWP"
set __local_ARG_FOUND=1
)
if /I "%1"=="uwp" (
set "__VCVARSALL_STORE=-app_platform=UWP"
set __local_ARG_FOUND=1
)
if "%__local_ARG_FOUND%" NEQ "1" (
set /A __VCVARSALL_PARSE_ERROR=__VCVARSALL_PARSE_ERROR+1
if "%2"=="" (
@echo [ERROR:%~nx0] Invalid argument found : %1
) else (
@echo [ERROR:%~nx0] Invalid argument found : %1=%2
)
)
set __local_ARG_FOUND=
exit /B 0
:usage_error
echo [ERROR:%~nx0] Error in script usage. The correct usage is:
goto :usage
:usage
echo Syntax:
echo %~nx0 [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
echo where :
echo [arch]: x86 ^| amd64 ^| x86_amd64 ^| x86_arm ^| x86_arm64 ^| amd64_x86 ^| amd64_arm ^| amd64_arm64
echo [platform_type]: {empty} ^| store ^| uwp
echo [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
echo [vc_version] : {none} for default VS 2017 VC++ compiler toolset ^|
echo "14.0" for VC++ 2015 Compiler Toolset ^|
echo "14.1x" for the latest 14.1x.yyyyy toolset installed (e.g. "14.11") ^|
echo "14.1x.yyyyy" for a specific full version number (e.g. 14.11.25503)
echo [spectre_mode] : {none} for default VS 2017 libraries without spectre mitigations ^|
echo "spectre" for VS 2017 libraries with spectre mitigations
echo:
echo The store parameter sets environment variables to support Universal Windows Platform application
echo development and is an alias for 'uwp'.
echo:
echo For example:
echo %~nx0 x86_amd64
echo %~nx0 x86_amd64 10.0.10240.0
echo %~nx0 x86_arm uwp 10.0.10240.0
echo %~nx0 x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
echo %~nx0 x64 8.1
echo %~nx0 x64 store 8.1
echo:
echo Please make sure either Visual Studio or C++ Build SKU is installed.
goto :end
:missing
echo The specified configuration type is missing. The tools for the
echo configuration might not be installed.
goto :end
:end
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_CLEAN_ENV=
set VSCMD_VCVARSALL_INIT=
set __VCVARSALL_VSDEVCMD_ARGS=
set __VCVARSALL_HELP=
set __VCVARSALL_VER=
set __VCVARSALL_SPECTRE=
这样问题就解决了,可以顺利Debug
解决过程
(1)在一开始debug的时候我遇到了以下问题: Could not set up the environment for Microsoft Visual Studio using
nvcc fatal : Could not set up the environment for Microsoft Visual Studio using 'C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/../../../../../../../VC/Auxiliary/Build/vcvars64.bat'
我在网上找了好久的解决办法,国外的社区还有国内的社区,都没有详细可靠的解决方案,总结下来大致的方法包括检查系统的环境变量,CUDA的路径不能有空格,nvcc的输入太长等,但这些都不能解决这个问题。
(2)在尝试了很长时间之后,我还是决定根据报错信息入手,我找到了出现问题的vcvars64.bat这个文件,默认路径在:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build
并用记事本打开,其中的内容如下,只有这一行:
@call "%~dp0vcvarsall.bat" x64 %*
(3)之后我在QT的工具->选项->Kits->编译器,发现QT中的编译器使用都是与上述vcvars64.bat同一路径下的vcvarsall.bat文件。
(4)因此我首先想要改变我的.pro文件调用vcvars文件的代码,有关的代码如下:
SYSTEM_TYPE = 64
改变等号右边的值,可以改变调试时使用的vavars文件的后缀,但是他只能接收数字输入,所以也无法使其调用vcvarsall.bat,因此只能另辟蹊径,考虑将vcvarsall.bat的内容复制到vcvars64.bat,达到调用vcvarsall.bat的效果。文章来源:https://www.toymoban.com/news/detail-438846.html
(5)最终解决方案,用记事本打开vcvarsall.bat,复制其中的代码到vcvars64.bat文章来源地址https://www.toymoban.com/news/detail-438846.html
到了这里,关于CUDA配置环境(三):nvcc fatal : Could not set up the environment for Microsoft Visual Studio using 已解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!