解决在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中的内容为:
1 @if not "%VSCMD_DEBUG%" GEQ "3" echo off
2
3 @REM -------------------------------------------------------------------------
4 :parse_args
5 set __VCVARSALL_STORE=
6 set __VCVARSALL_WINSDK=
7 set __VCVARSALL_PARSE_ERROR=
8 set __VCVARSALL_TARGET_ARCH=
9 set __VCVARSALL_HOST_ARCH=
10 set __VCVARSALL_VER=
11
12 @REM Parse command line arguments. This implementation does not care about
13 @REM argument order.
14
15 if "%VSCMD_DEBUG%" GEQ "1" (
16 @echo [DEBUG:%~nx0] init with arguments '%*'
17 )
18
19 set __VCVARSALL_PARSE_ERROR=0
20 set "__VCVARSALL_ARGS_LIST=%*"
21 call :parse_loop
22 set __VCVARSALL_ARGS_LIST=
23
24 if "%VSCMD_DEBUG%" GEQ "1" (
25 @echo [DEBUG:%~nx0] Command line parse completed with values:
26 @echo [DEBUG:%~nx0] __VCVARSALL_TARGET_ARCH='%__VCVARSALL_TARGET_ARCH%'
27 @echo [DEBUG:%~nx0] __VCVARSALL_HOST_ARCH='%__VCVARSALL_HOST_ARCH%'
28 @echo [DEBUG:%~nx0] __VCVARSALL_WINSDK='%__VCVARSALL_WINSDK%'
29 @echo [DEBUG:%~nx0] __VCVARSALL_STORE='%__VCVARSALL_STORE%'
30 @echo [DEBUG:%~nx0] __VCVARSALL_HELP='%__VCVARSALL_HELP%'
31 @echo [DEBUG:%~nx0] __VCVARSALL_PARSE_ERROR='%__VCVARSALL_PARSE_ERROR%'
32 )
33
34 if "%__VCVARSALL_CLEAN_ENV%" NEQ "" goto :call_vsdevcmd
35 if "%__VCVARSALL_PARSE_ERROR%" NEQ "0" goto :usage_error
36 if "%__VCVARSALL_HELP%" NEQ "" goto :usage
37
38 @REM -------------------------------------------------------------------------
39 :check_platform
40 @REM This script is installed to ...\VC\Auxiliary\Build.
41 @REM vsdevcmd is installed to ...\Common7\Tools.
42 if not exist "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" goto missing
43
44 @REM Assemble the arguments to pass to vsdevcmd.bat
45 if "%__VCVARSALL_TARGET_ARCH%" == "" goto :usage_error
46 if "%__VCVARSALL_HOST_ARCH%" == "" goto :usage_error
47
48 set "__VCVARSALL_VSDEVCMD_ARGS=-arch=%__VCVARSALL_TARGET_ARCH% -host_arch=%__VCVARSALL_HOST_ARCH%"
49 if "%__VCVARSALL_WINSDK%" NEQ "" (
50 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -winsdk=%__VCVARSALL_WINSDK%"
51 )
52 if "%__VCVARSALL_STORE%" NEQ "" (
53 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -app_platform=UWP"
54 )
55 if "%__VCVARSALL_VER%" NEQ "" (
56 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_ver=%__VCVARSALL_VER%"
57 )
58 if "%__VCVARSALL_SPECTRE%" NEQ "" (
59 set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_spectre_libs=%__VCVARSALL_SPECTRE%"
60 )
61
62 goto :call_vsdevcmd
63
64 @REM -------------------------------------------------------------------------
65 @REM Call vsdevcmd.bat to setup the command prompt environment
66
67 :call_vsdevcmd
68
69 @REM This temporary environment variable is used to control setting of VC++
70 @REM command prompt-specific environment variables that should not be set
71 @REM by the VS Developer Command prompt (specifically vsdevcmd\ext\vcvars.bat).
72 @REM The known case this effects is the Platform environment variable, which
73 @REM will override platform target for .NET builds.
74 set VSCMD_VCVARSALL_INIT=1
75
76 @REM Special handling for the /clean_env argument
77 if "%__VCVARSALL_CLEAN_ENV%" NEQ "" (
78 call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" /clean_env
79 goto :end
80 )
81
82 call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" %__VCVARSALL_VSDEVCMD_ARGS%
83
84 if "%ERRORLEVEL%"=="0" (
85 @REM Print the target command prompt architecture...
86 if "%__VCVARSALL_HOST_ARCH%" NEQ "%__VCVARSALL_TARGET_ARCH%" (
87 echo [%~nx0] Environment initialized for: '%__VCVARSALL_HOST_ARCH%_%__VCVARSALL_TARGET_ARCH%'
88 ) else (
89 echo [%~nx0] Environment initialized for: '%__VCVARSALL_TARGET_ARCH%'
90 )
91 )
92 goto :end
93
94 :parse_loop
95 for /F "tokens=1,* delims= " %%a in ("%__VCVARSALL_ARGS_LIST%") do (
96 if "%VSCMD_DEBUG%" GEQ "2" (
97 @echo [DEBUG:%~nx0] inner argument {%%a}
98 )
99 call :parse_argument %%a
100 set "__VCVARSALL_ARGS_LIST=%%b"
101 goto :parse_loop
102 )
103
104 exit /B 0
105
106 :parse_argument
107
108 @REM called by :parse_loop and expects the arguments to either be:
109 @REM 1. a single argument in %1
110 @REM 2. an argument pair from the command line specified as '%1=%2'
111
112 set __local_ARG_FOUND=
113 @REM Architecture
114 if /I "%1"=="x86" (
115 set __VCVARSALL_TARGET_ARCH=x86
116 set __VCVARSALL_HOST_ARCH=x86
117 set __local_ARG_FOUND=1
118 )
119 if /I "%1"=="x86_amd64" (
120 set __VCVARSALL_TARGET_ARCH=x64
121 set __VCVARSALL_HOST_ARCH=x86
122 set __local_ARG_FOUND=1
123 )
124 if /I "%1"=="x86_x64" (
125 set __VCVARSALL_TARGET_ARCH=x64
126 set __VCVARSALL_HOST_ARCH=x86
127 set __local_ARG_FOUND=1
128 )
129 if /I "%1"=="x86_arm" (
130 set __VCVARSALL_TARGET_ARCH=arm
131 set __VCVARSALL_HOST_ARCH=x86
132 set __local_ARG_FOUND=1
133 )
134 if /I "%1"=="x86_arm64" (
135 set __VCVARSALL_TARGET_ARCH=arm64
136 set __VCVARSALL_HOST_ARCH=x86
137 set __local_ARG_FOUND=1
138 )
139 if /I "%1"=="amd64" (
140 set __VCVARSALL_TARGET_ARCH=x64
141 set __VCVARSALL_HOST_ARCH=x64
142 set __local_ARG_FOUND=1
143 )
144 if /I "%1"=="x64" (
145 set __VCVARSALL_TARGET_ARCH=x64
146 set __VCVARSALL_HOST_ARCH=x64
147 set __local_ARG_FOUND=1
148 )
149 if /I "%1"=="amd64_x86" (
150 set __VCVARSALL_TARGET_ARCH=x86
151 set __VCVARSALL_HOST_ARCH=x64
152 set __local_ARG_FOUND=1
153 )
154 if /I "%1"=="x64_x86" (
155 set __VCVARSALL_TARGET_ARCH=x86
156 set __VCVARSALL_HOST_ARCH=x64
157 set __local_ARG_FOUND=1
158 )
159 if /I "%1"=="amd64_arm" (
160 set __VCVARSALL_TARGET_ARCH=arm
161 set __VCVARSALL_HOST_ARCH=x64
162 set __local_ARG_FOUND=1
163 )
164 if /I "%1"=="x64_arm" (
165 set __VCVARSALL_TARGET_ARCH=arm
166 set __VCVARSALL_HOST_ARCH=x64
167 set __local_ARG_FOUND=1
168 )
169 if /I "%1"=="amd64_arm64" (
170 set __VCVARSALL_TARGET_ARCH=arm64
171 set __VCVARSALL_HOST_ARCH=x64
172 set __local_ARG_FOUND=1
173 )
174 if /I "%1"=="x64_arm64" (
175 set __VCVARSALL_TARGET_ARCH=arm64
176 set __VCVARSALL_HOST_ARCH=x64
177 set __local_ARG_FOUND=1
178 )
179 if /I "%1"=="-vcvars_ver" (
180 set "__VCVARSALL_VER=%2"
181 set __local_ARG_FOUND=1
182 )
183 if /I "%1"=="/vcvars_ver" (
184 set "__VCVARSALL_VER=%2"
185 set __local_ARG_FOUND=1
186 )
187 if /I "%1"=="--vcvars_ver" (
188 set "__vcvarsall_ver=%2"
189 set __local_ARG_FOUND=1
190 )
191 if /I "%1"=="-vcvars_spectre_libs" (
192 set "__VCVARSALL_SPECTRE=%2"
193 set __local_ARG_FOUND=1
194 )
195 if /I "%1"=="/vcvars_spectre_libs" (
196 set "__VCVARSALL_SPECTRE=%2"
197 set __local_ARG_FOUND=1
198 )
199 if /I "%1"=="--vcvars_spectre_libs" (
200 set "__vcvarsall_SPECTRE=%2"
201 set __local_ARG_FOUND=1
202 )
203 if /I "%1"=="help" (
204 set __VCVARSALL_HELP=1
205 set __local_ARG_FOUND=1
206 )
207 if /I "%1"=="/help" (
208 set __VCVARSALL_HELP=1
209 set __local_ARG_FOUND=1
210 )
211 if /I "%1"=="-help" (
212 set __VCVARSALL_HELP=1
213 set __local_ARG_FOUND=1
214 )
215 if /I "%1"=="/?" (
216 set __VCVARSALL_HELP=1
217 set __local_ARG_FOUND=1
218 )
219 if /I "%1"=="-?" (
220 set __VCVARSALL_HELP=1
221 set __local_ARG_FOUND=1
222 )
223
224 @REM -- /clean_env --
225 @REM Mostly used for internal testing to restore the state of
226 @REM the command line environment to its state prior to vcvarsall.bat
227 @REM being executed.
228 if /I "%1"=="/clean_env" (
229 set __VCVARSALL_CLEAN_ENV=/clean_env
230 set __local_ARG_FOUND=1
231 )
232 if /I "%1"=="-clean_env" (
233 set __VCVARSALL_CLEAN_ENV=/clean_env
234 set __local_ARG_FOUND=1
235 )
236
237 @REM Windows SDK Version
238 if /I "%1"=="8.1" (
239 set "__VCVARSALL_WINSDK=8.1"
240 set __local_ARG_FOUND=1
241 )
242
243 set __temp1=%1
244 if /I "%__temp1:~0,3%"=="10." (
245 set "__VCVARSALL_WINSDK=%1"
246 set __local_ARG_FOUND=1
247 )
248 set __temp1=
249
250 @REM Store/UWP
251 if /I "%1"=="store" (
252 set "__VCVARSALL_STORE=-app_platform=UWP"
253 set __local_ARG_FOUND=1
254 )
255 if /I "%1"=="uwp" (
256 set "__VCVARSALL_STORE=-app_platform=UWP"
257 set __local_ARG_FOUND=1
258 )
259
260 if "%__local_ARG_FOUND%" NEQ "1" (
261 set /A __VCVARSALL_PARSE_ERROR=__VCVARSALL_PARSE_ERROR+1
262 if "%2"=="" (
263 @echo [ERROR:%~nx0] Invalid argument found : %1
264 ) else (
265 @echo [ERROR:%~nx0] Invalid argument found : %1=%2
266 )
267 )
268 set __local_ARG_FOUND=
269 exit /B 0
270
271 :usage_error
272 echo [ERROR:%~nx0] Error in script usage. The correct usage is:
273 goto :usage
274
275 :usage
276 echo Syntax:
277 echo %~nx0 [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
278 echo where :
279 echo [arch]: x86 ^| amd64 ^| x86_amd64 ^| x86_arm ^| x86_arm64 ^| amd64_x86 ^| amd64_arm ^| amd64_arm64
280 echo [platform_type]: {empty} ^| store ^| uwp
281 echo [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
282 echo [vc_version] : {none} for default VS 2017 VC++ compiler toolset ^|
283 echo "14.0" for VC++ 2015 Compiler Toolset ^|
284 echo "14.1x" for the latest 14.1x.yyyyy toolset installed (e.g. "14.11") ^|
285 echo "14.1x.yyyyy" for a specific full version number (e.g. 14.11.25503)
286 echo [spectre_mode] : {none} for default VS 2017 libraries without spectre mitigations ^|
287 echo "spectre" for VS 2017 libraries with spectre mitigations
288 echo:
289 echo The store parameter sets environment variables to support Universal Windows Platform application
290 echo development and is an alias for 'uwp'.
291 echo:
292 echo For example:
293 echo %~nx0 x86_amd64
294 echo %~nx0 x86_amd64 10.0.10240.0
295 echo %~nx0 x86_arm uwp 10.0.10240.0
296 echo %~nx0 x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
297 echo %~nx0 x64 8.1
298 echo %~nx0 x64 store 8.1
299 echo:
300 echo Please make sure either Visual Studio or C++ Build SKU is installed.
301 goto :end
302
303 :missing
304 echo The specified configuration type is missing. The tools for the
305 echo configuration might not be installed.
306 goto :end
307
308 :end
309 set __VCVARSALL_TARGET_ARCH=
310 set __VCVARSALL_HOST_ARCH=
311 set __VCVARSALL_STORE=
312 set __VCVARSALL_WINSDK=
313 set __VCVARSALL_PARSE_ERROR=
314 set __VCVARSALL_CLEAN_ENV=
315 set VSCMD_VCVARSALL_INIT=
316 set __VCVARSALL_VSDEVCMD_ARGS=
317 set __VCVARSALL_HELP=
318 set __VCVARSALL_VER=
319 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的效果。
(5)最终解决方案,用记事本打开vcvarsall.bat,复制其中的代码到vcvars64.bat