首页 > 技术文章 > Visual Studio 命令提示

91Kesson 2014-10-16 11:31 原文

MSDN中介绍:

①Visual Studio 开发人员命令提示会自动设置环境变量,这些变量使你能够轻松使用 .NET Framework 工具。②开发人员命令提示将与 Visual Studio 一起安装。

 从Visual Studio 2010 开始,你可看到多个命令提示,具体取决于你安装的 Visual Studio 和任何其他 SDK 的版本。例如,Visual Studio 的 64 位版本同时提供 32 位和 64 位命令提示。(大多数工具的 32 位和 64 位版本都相同;但少数工具针对具体的 32 位和 64 位环境做了一些改变。)

对于 Windows 7 中的 Visual Studio(任何版本)        

  • 选择“开始”,然后选择“所有程序”、“Microsoft Visual Studio”、“Visual Studio Tools”和“Visual Studio 命令提示”。

我们右键“Visual Studio 命令提示”->属性可看到执行“Visual Studio 命令提示”实际上是运行了一个.bat文件 加上了不同的对应参数:

“Virtual Studio命令提示(2010)"对应于:

  %comspec% /k ""D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86

“Virtual Studio x64 兼容工具命令提示”对应于:

  %comspec% /k ""D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_amd64

“Virtual Studio Itanium 兼容工具命令提示”对应于:

  %comspec% /k ""D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_ia64

我们打开""D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" 文件:

 1 @echo off
 2 if "%1" == "" goto x86
 3 if not "%2" == "" goto usage
 4 
 5 if /i %1 == x86       goto x86
 6 if /i %1 == amd64     goto amd64
 7 if /i %1 == x64       goto amd64
 8 if /i %1 == ia64      goto ia64
 9 if /i %1 == x86_amd64 goto x86_amd64
10 if /i %1 == x86_ia64  goto x86_ia64
11 goto usage
12 
13 :x86
14 if not exist "%~dp0bin\vcvars32.bat" goto missing
15 call "%~dp0bin\vcvars32.bat"
16 goto :eof
17 
18 :amd64
19 if not exist "%~dp0bin\amd64\vcvars64.bat" goto missing
20 call "%~dp0bin\amd64\vcvars64.bat"
21 goto :eof
22 
23 :ia64
24 if not exist "%~dp0bin\ia64\vcvars64.bat" goto missing
25 call "%~dp0bin\ia64\vcvars64.bat"
26 goto :eof
27 
28 :x86_amd64
29 if not exist "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat" goto missing
30 call "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat"
31 goto :eof
32 
33 :x86_ia64
34 if not exist "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat" goto missing
35 call "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat"
36 goto :eof
37 
38 :usage
39 echo Error in script usage. The correct usage is:
40 echo     %0 [option]
41 echo where [option] is: x86 ^| ia64 ^| amd64 ^| x86_amd64 ^| x86_ia64
42 echo:
43 echo For example:
44 echo     %0 x86_ia64
45 goto :eof
46 
47 :missing
48 echo The specified configuration type is missing.  The tools for the
49 echo configuration might not be installed.
50 goto :eof
View Code

发现针对不同的参数,vcvarsall.bat打开了不同的文件(比如, x86参数是调用了"%~dp0bin\vcvars32.bat“文件;

我们打开"%~dp0bin\vcvars32.bat“文件进行查看:

  1 @echo Setting environment for using Microsoft Visual Studio 2010 x86 tools.
  2 
  3 @call :GetVSCommonToolsDir
  4 @if "%VS100COMNTOOLS%"=="" goto error_no_VS100COMNTOOLSDIR
  5 
  6 @call "%VS100COMNTOOLS%VCVarsQueryRegistry.bat" 32bit No64bit
  7 
  8 @if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
  9 @if "%FrameworkDir32%"=="" goto error_no_FrameworkDIR32
 10 @if "%FrameworkVersion32%"=="" goto error_no_FrameworkVer32
 11 @if "%Framework35Version%"=="" goto error_no_Framework35Version
 12 
 13 @set FrameworkDir=%FrameworkDir32%
 14 @set FrameworkVersion=%FrameworkVersion32%
 15 
 16 @if not "%WindowsSdkDir%" == "" (
 17     @set "PATH=%WindowsSdkDir%bin\NETFX 4.0 Tools;%WindowsSdkDir%bin;%PATH%"
 18     @set "INCLUDE=%WindowsSdkDir%include;%INCLUDE%"
 19     @set "LIB=%WindowsSdkDir%lib;%LIB%"
 20 )
 21 
 22 @rem
 23 @rem Root of Visual Studio IDE installed files.
 24 @rem
 25 @set DevEnvDir=%VSINSTALLDIR%Common7\IDE\
 26 
 27 @rem PATH
 28 @rem ----
 29 @if exist "%VSINSTALLDIR%Team Tools\Performance Tools" (
 30     @set "PATH=%VSINSTALLDIR%Team Tools\Performance Tools;%PATH%"
 31 )
 32 @if exist "%ProgramFiles%\HTML Help Workshop" set PATH=%ProgramFiles%\HTML Help Workshop;%PATH%
 33 @if exist "%ProgramFiles(x86)%\HTML Help Workshop" set PATH=%ProgramFiles(x86)%\HTML Help Workshop;%PATH%
 34 @if exist "%VCINSTALLDIR%VCPackages" set PATH=%VCINSTALLDIR%VCPackages;%PATH%
 35 @set PATH=%FrameworkDir%%Framework35Version%;%PATH%
 36 @set PATH=%FrameworkDir%%FrameworkVersion%;%PATH%
 37 @set PATH=%VSINSTALLDIR%Common7\Tools;%PATH%
 38 @if exist "%VCINSTALLDIR%BIN" set PATH=%VCINSTALLDIR%BIN;%PATH%
 39 @set PATH=%DevEnvDir%;%PATH%
 40 
 41 @if exist "%VSINSTALLDIR%VSTSDB\Deploy" (
 42     @set "PATH=%VSINSTALLDIR%VSTSDB\Deploy;%PATH%"
 43 )
 44 
 45 @if not "%FSHARPINSTALLDIR%" == "" (
 46     @set "PATH=%FSHARPINSTALLDIR%;%PATH%"
 47 )
 48 
 49 @rem INCLUDE
 50 @rem -------
 51 @if exist "%VCINSTALLDIR%ATLMFC\INCLUDE" set INCLUDE=%VCINSTALLDIR%ATLMFC\INCLUDE;%INCLUDE%
 52 @if exist "%VCINSTALLDIR%INCLUDE" set INCLUDE=%VCINSTALLDIR%INCLUDE;%INCLUDE%
 53 
 54 @rem LIB
 55 @rem ---
 56 @if exist "%VCINSTALLDIR%ATLMFC\LIB" set LIB=%VCINSTALLDIR%ATLMFC\LIB;%LIB%
 57 @if exist "%VCINSTALLDIR%LIB" set LIB=%VCINSTALLDIR%LIB;%LIB%
 58 
 59 @rem LIBPATH
 60 @rem -------
 61 @if exist "%VCINSTALLDIR%ATLMFC\LIB" set LIBPATH=%VCINSTALLDIR%ATLMFC\LIB;%LIBPATH%
 62 @if exist "%VCINSTALLDIR%LIB" set LIBPATH=%VCINSTALLDIR%LIB;%LIBPATH%
 63 @set LIBPATH=%FrameworkDir%%Framework35Version%;%LIBPATH%
 64 @set LIBPATH=%FrameworkDir%%FrameworkVersion%;%LIBPATH%
 65 
 66 @goto end
 67 
 68 @REM -----------------------------------------------------------------------
 69 :GetVSCommonToolsDir
 70 @set VS100COMNTOOLS=
 71 @call :GetVSCommonToolsDirHelper32 HKLM > nul 2>&1
 72 @if errorlevel 1 call :GetVSCommonToolsDirHelper32 HKCU > nul 2>&1
 73 @if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKLM > nul 2>&1
 74 @if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKCU > nul 2>&1
 75 @exit /B 0
 76 
 77 :GetVSCommonToolsDirHelper32
 78 @for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v "10.0"') DO (
 79     @if "%%i"=="10.0" (
 80         @SET "VS100COMNTOOLS=%%k"
 81     )
 82 )
 83 @if "%VS100COMNTOOLS%"=="" exit /B 1
 84 @SET "VS100COMNTOOLS=%VS100COMNTOOLS%Common7\Tools\"
 85 @exit /B 0
 86 
 87 :GetVSCommonToolsDirHelper64
 88 @for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "10.0"') DO (
 89     @if "%%i"=="10.0" (
 90         @SET "VS100COMNTOOLS=%%k"
 91     )
 92 )
 93 @if "%VS100COMNTOOLS%"=="" exit /B 1
 94 @SET "VS100COMNTOOLS=%VS100COMNTOOLS%Common7\Tools\"
 95 @exit /B 0
 96 
 97 @REM -----------------------------------------------------------------------
 98 :error_no_VS100COMNTOOLSDIR
 99 @echo ERROR: Cannot determine the location of the VS Common Tools folder.
100 @goto end
101 
102 :error_no_VSINSTALLDIR
103 @echo ERROR: Cannot determine the location of the VS installation.
104 @goto end
105 
106 :error_no_FrameworkDIR32
107 @echo ERROR: Cannot determine the location of the .NET Framework 32bit installation.
108 @goto end
109 
110 :error_no_FrameworkVer32
111 @echo ERROR: Cannot determine the version of the .NET Framework 32bit installation.
112 @goto end
113 
114 :error_no_Framework35Version
115 @echo ERROR: Cannot determine the .NET Framework 3.5 version.
116 @goto end
117 
118 :end
vcvars32.bat

一堆@set PATH 、@set LIBPATH之类的..看的有点晕....

备注:

①"%~dp0": 0念 零 ,代表你的批处理本身。 d p是FOR 命令的扩展。

%~f0  将 %I 扩展到一个完全合格的路径名
%~d0  仅将 %I 扩展到一个驱动器号
%~p0  仅将 %I 扩展到一个路径搜索
%~n0  仅将 %I 扩展到一个文件名
%~x0  仅将 %I 扩展到一个文件扩展名
%~s0  扩展的路径只含有短名
%~a0  将 %I 扩展到文件的文件属性
%~t0  将 %I 扩展到文件的日期/时间
%~z0  将 %I 扩展到文件的大小

比如:d:\123\a.bat;

%~p0是d:\

%~pd0就是d:\123
%0就是 d:\123\a.bat

 

推荐阅读