缘由
需要使用vcpkg中低版本的第三方库,下载vcpkg后,回退至指定版本,运行bootstrap-vcpkg.bat生成vcpkg.exe时,命令行窗口总是一闪而过,但是vcpkg.exe却没有生成。
添加pause,查看错误
编辑bootstrap-vcpkg.bat,在末尾添加pause,使之暂停,显示错误
问题1:网络问题
下载失败,多尝试几遍或者使用其他魔法
问题2:编译失败
继续查看bootstrap-vcpkg.bat
@echo off
powershell.exe -NoProfile -ExecutionPolicy Bypass "& {& '%~dp0scripts\bootstrap.ps1'}"
pause
打开scripts\bootstrap.ps1查看,调试并分析,MSBuild.exe目录可能与以下代码有关
$msbuildExeWithPlatformToolset = & $scriptsDir\findAnyMSBuildWithCppPlatformToolset.ps1 $withVSPath
$msbuildExe = $msbuildExeWithPlatformToolset[0]
$platformToolset = $msbuildExeWithPlatformToolset[1]
$windowsSDK = & $scriptsDir\getWindowsSDK.ps1
继续打开findAnyMSBuildWithCppPlatformToolset.ps1
[CmdletBinding()]
param([Parameter(Mandatory=$False)][string]$withVSPath = ""
)Set-StrictMode -Version Latest
$scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition$withVSPath = $withVSPath -replace "\\$" # Remove potential trailing backslash$VisualStudioInstallationInstances = & $scriptsDir\findVisualStudioInstallationInstances.ps1
if ($VisualStudioInstallationInstances -eq $null)
{throw "Could not find Visual Studio. VS2015 or VS2017 (with C++) needs to be installed."
}Write-Verbose "VS Candidates:`n`r$([system.String]::Join([Environment]::NewLine, $VisualStudioInstallationInstances))"
foreach ($instanceCandidateWithEOL in $VisualStudioInstallationInstances)
{$instanceCandidate = $instanceCandidateWithEOL -replace "<sol>::" -replace "::<eol>"Write-Verbose "Inspecting: $instanceCandidate"$split = $instanceCandidate -split "::"# $preferenceWeight = $split[0]# $releaseType = $split[1]$version = $split[2]$path = $split[3]if ($withVSPath -ne "" -and $withVSPath -ne $path){Write-Verbose "Skipping: $instanceCandidate"continue}$majorVersion = $version.Substring(0,2);if ($majorVersion -eq "15"){$VCFolder= "$path\VC\Tools\MSVC\"if (Test-Path $VCFolder){Write-Verbose "Picking: $instanceCandidate"return "$path\MSBuild\15.0\Bin\MSBuild.exe", "v141"}}if ($majorVersion -eq "14"){$clExe= "$path\VC\bin\cl.exe"if (Test-Path $clExe){Write-Verbose "Picking: $instanceCandidate"$programFilesPath = & $scriptsDir\getProgramFiles32bit.ps1return "$programFilesPath\MSBuild\14.0\Bin\MSBuild.exe", "v140"}}
}throw "Could not find MSBuild version with C++ support. VS2015 or VS2017 (with C++) needs to be installed."
可以看到由于版本较旧,只支持了VS2015 和 VS2017,但是我的机子安装的是VS2022,所以导致了MSBuild.exe目录查找失败
尝试修改返回值
修改成自己机子上的VS安装目录
[CmdletBinding()]
param([Parameter(Mandatory=$False)][string]$withVSPath = ""
)return "D:\CodingTools\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe", "v143"以下省略...
运行,编译失败,可能是编译器太新,使用的C++版本不兼容,继续修改
这里我修改成了2017的编译器版本,如果没有安装2015或者2017,可能需要在已安装的机子上运行后拷贝过来,或者修改VS,安装对应的编译器
return "D:\CodingTools\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe", "v141"
编译成功,成功生成vcpkg.exe
注意
这个方法只试用与刚clone的vcpkg,如果克隆后有安装过第三方库,再使用改方法,可能会导致vcpkg报错,版本不兼容