日期:2014-05-16  浏览次数:20972 次

不安装Visual Studio,只用Windows SDK搭建VC环境

首先你需要下载的是

Microsoft Windows SDK for Windows 7 and .NET Framework 4. 下载以后你得到的是一个网络安装文件,确保网络的连接,安装Windows SDK 7.1 (注意得到的Windows SDK 版本是7.1,这和我们安装Vsiual Studio得到的SDK 版本v7.0A是不一样的);

?

安装界面如下:



?
安装结束以后你应该得到如下的

1. 在c:\Program Files\Microsoft Visual Studio 10.0,你会看到不完全的Visual Studio的安装(主要是VC的库和头文件)

2.在C:\Program Files\Microsoft SDKs\Windows\v7.1,你会看到Windows SDK的安装。

3. 在C:\Windows\Microsoft.NET,你会看到各个版本的Framework 的安装

4. 在HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VC7,你会看到如下的值

? ?“10.0” ?“C:\Program Files\Microsoft Visual Studio 10.0\VC\”

? ?“FrameworkDir32" “C:\Windows\Microsoft.NET\Framework\”

? ?“FrameworkDir64" “C:\Windows\Microsoft.NET\Framework64”

? ?"Framework32Ver” “v4.0.30319”

? ?"Framework64Ver” “v4.0.30319”

?

但是这样你还是不能运行vc++环境。首先你不能再PATH路径找到nmake,cl,而且,即使把cl加到了PATH路径中, 你也不能正确的运行编译,你需要一个脚本来初始化设置;

?

c:\Program Files\Microsoft Visual Studio 10.0中,有两个文件可以用来初始化环境,一个是Common7\Tools\vsvars32.bat,另一个是VC\vcvarsall.bat;

但是这两个文件需要更改,因为

1. 该文件查询注册HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7?表找寻VS的安装目录,但是HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7只有在完全安装Visual studio以后才存在。你需要的是通过HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VC7,通过去掉结尾的VC\来间接的找到VS的安装目录。

:GetVSCommonToolsDirHelper32
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v "10.0"') DO (
	@if "%%i"=="10.0" (
		@SET "VS100COMNTOOLS=%%k"
	)
)

?

2.文件查询HKLM\SOFTWARE\Microsoft SDKs\Windows\v7.0A来确定Windows SDK的安装目录,但是 该目录是安装Visual Studio时候才会安装的较低版本,安装了Windows SDK和Framework以后的Windows SDK版本更高,是v7.1;

:GetWindowsSdkDirHelper
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A" /v "InstallationFolder"') DO (
	@if "%%i"=="InstallationFolder" (
		@SET "WindowsSdkDir=%%k"
	)
)

?

为了设置vc环境变量,我创建了my_vcvarsall.bat,内容如下:

?

@echo off
if "%1" == "" goto x86
if not "%2" == "" goto usage

if /i %1 == x86       goto x86
if /i %1 == amd64     goto amd64
if /i %1 == x64       goto amd64
if /i %1 == ia64      goto ia64
if /i %1 == x86_amd64 goto x86_amd64
if /i %1 == x86_ia64  goto x86_ia64
goto usage

:x86
if not exist "%~dp0bin\my_vcvars32.bat" goto missing
call "%~dp0bin\my_vcvars32.bat"
goto :eof

:amd64
if not exist "%~dp0bin\amd64\vcvars64.bat" goto missing
call "%~dp0bin\amd64\vcvars64.bat"
goto :eof

:ia64
if not exist "%~dp0bin\ia64\vcvars64.bat" goto missing
call "%~dp0bin\ia64\vcvars64.bat"
goto :eof

:x86_amd64
if not exist "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat" goto missing
call "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat"
goto :eof

:x86_ia64
if not exist "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat" goto missing
call "%~dp0bin\x86_ia64\vcvarsx86_ia64.bat"
goto :eof

:usage
echo Error in script usage. The correct usage is:
echo     %0 [option]
echo where [option] is: x86 ^| ia64 ^| amd64 ^| x86_amd64 ^| x86_ia64
echo:
echo For example:
echo     %0 x86_ia64
goto :eof

:missing
echo The specified configuration type is missing.  The tools for the
echo configuration might not be installed.
goto :eof

?

该文件会调