緣由
需要使用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報錯,版本不兼容