最近因需要嘗試編譯了一次Vbox,官方論壇上只有32位環(huán)境的編譯教程,所以這次在64位、特別是中文環(huán)境上遇到不少坑。這里總結(jié)出來,希望對(duì)后面的人有幫助。
下載編譯所需工具
編譯所需工具可參考Windows build instructions
該地址為官網(wǎng)的編譯操作指南,寫得比較泛泛而談,簡(jiǎn)單參考一下即可。我編譯64位VBox用到的工具如下:
- Visual Studio 2010 加 Visual Studio 2010 Service pack 1也就是sp1
- Windows Platform SDK v7.1
- Windows Driver Development Kit (WDK) v7.1
- MingW-w64
- Qt-4.7.3
- SDL-1.2.15
- cURL-7.49.1
- OpenSSL-1.0.1t
- ActivePerl
- Python 2.x
- VirtualBox 5.0.0
還有一些東西官網(wǎng)的操作指南有提到,但是不是必須的,在這里我沒有用到。
搭建編譯環(huán)境
- 安裝Windows Platform SDK
- 安裝WDK
- 安裝Visual Studio 2010
- 安裝Visual Studio 2010 Service pack 1
- 安裝python和ActivePerl
最好在安裝VS2010之前安裝SDK和WDK,不然安裝時(shí)可能會(huì)出顯示.Net框架沖突的錯(cuò)誤。
開始編譯
從現(xiàn)在開始接下來一路是坑
解壓文件
- 新建一個(gè)Vbox的編譯目錄,如:E:\Vbox,將qt-everywhere-opensource-src-4.7.3,SDL-1.2.15,mingw64,openssl-1.0.1t,curl-7.49.1解壓到E\Vbox下。將openssl和curl復(fù)制一份,重命名為openssl-1.0.1t-86,openssl-1.0.1t-64,curl-7.49.1-86,curl-7.49.1-64。
├─curl-7.49.1-64
│
├─curl-7.49.1-86
│
├─mingw64
│
├─openssl-1.0.1t-64
│
├─openssl-1.0.1t-86
│
├─qt-everywhere-opensource-src-4.7.3
│
├─SDL-1.2.15
│
├─VirtualBox-5.0.0
編譯QT
- 將Qt目錄加入環(huán)境變量,如:E:\Vbox\qt-4.7.3\bin
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 切換到Qt根目錄,如:E:\Vbox\qt-4.7.3
- 命令行輸入
set QMAKESPEC=E:\Vbox\qt-everywhere-opensource-src-4.7.3\mkspecs\win32-msvc2010
configure -debug-and-release -opensource -platform win32-msvc2010
nmake
編譯OpenSSL-64
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 切換到openssl-64目錄,如:E:\Vbox\openssl-1.0.1t-64
- 命令行輸入
perl Configure VC-WIN64A --prefix=E:\Vbox\openssl-1.0.1t-64\output
ms\do_win64a
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
編譯結(jié)果將輸出到 E:\Vbox\openssl-1.0.1t-64\output
編譯OpenSSL-86
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio 命令提示(2010)
- 切換到openssl-86目錄,如:E:\Vbox\openssl-1.0.1t-86
- 命令行輸入
perl Configure VC-WIN32 --prefix=E:\Vbox\openssl-1.0.1t-86\output
ms\do_ms
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
編譯結(jié)果將輸出到 E:\Vbox\openssl-1.0.1t-86\output
編譯cURL-64
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 切換到cURL-64目錄,如:E:\Vbox\curl-7.49.1-64
- 命令行輸入
set OPENSSL_PATH=..\openssl-1.0.1t-64
pushd lib
nmake /f Makefile.vc10 cfg=release-dll MACHINE=x64
編譯結(jié)果將輸出到 E:\Vbox\curl-7.49.1-64\lib\release-dll
- 將E:\Vbox\curl-7.49.1-64\lib\release-dll\libcurl_imp.lib 重命名為 libcurl.lib
- 將E:\Vbox\curl-7.49.1-64\lib\release-dll\libcurl.lib和E:\Vbox\curl-7.49.1-64\lib\release-dll\libcurl.dll 復(fù)制到 E:\Vbox\curl-7.49.1-64
編譯cURL-86
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio 命令提示(2010)
- 切換到cURL-86目錄,如:E:\Vbox\curl-7.49.1-86
- 命令行輸入
set OPENSSL_PATH=..\openssl-1.0.1t-86
pushd lib
nmake /f Makefile.vc10 cfg=release-dll MACHINE=x86
編譯結(jié)果將輸出到 E:\Vbox\curl-7.49.1-86\lib\release-dll
- 將E:\Vbox\curl-7.49.1-86\lib\release-dll\libcurl_imp.lib 重命名為 libcurl.lib
- 將E:\Vbox\curl-7.49.1-86\lib\release-dll\libcurl.lib和E:\Vbox\curl-7.49.1-86\lib\release-dll\libcurl.dll 復(fù)制到 E:\Vbox\curl-7.49.1-86
編譯Virtual Box
- 在Vbox根目錄(如:E:\Vbox\VirtualBox-5.0.0)下建立LocalConfig.kmk
VBOX_WITH_HARDENING :=
SDK_VBOX_OPENSSL-x86_INCS := E:/Vbox/openssl-1.0.1t-86/output/include
SDK_VBOX_OPENSSL-x86_LIBS := E:/Vbox/openssl-1.0.1t-86/output/lib/ssleay32.lib E:/Vbox/openssl-1.0.1t-86/output/lib/libeay32.lib
SDK_VBOX_BLD_OPENSSL-x86_LIBS := $(SDK_VBOX_OPENSSL-x86_LIBS)
SDK_VBOX_LIBCURL-x86_INCS := E:/Vbox/curl-7.49.1-86/include
SDK_VBOX_LIBCURL-x86_LIBS.x86 := E:/Vbox/curl-7.49.1-86/lib/release-dll/libcurl.lib
- 復(fù)制E:\Vbox\curl-7.49.1-64\include\curl到E:\Vbox\VirtualBox-5.0.0\include\curl
- 復(fù)制E:\Vbox\openssl-1.0.1t-64\output\include\openssl到E:\Vbox\VirtualBox-5.0.0\include\openssl
- 修改E:\Vbox\VirtualBox-5.0.0\Config.kmk 190行
#VBOX_NM ?= $(PATH_TOOL_MINGW32)/bin/nm.exe -p
VBOX_NM ?= E:/Vbox/mingw64/bin/nm.exe -p
- 修改E:\Vbox\VirtualBox-5.0.0\configure.vbs
#line 1063
#if Shell(DosSlashes(strPathVC & "/bin/cl.exe"), True) <> 0 then
if Shell(DosSlashes(strPathVC & "/cl.exe"), True) <> 0 then
#line 1068
#if (InStr(1, g_strShellOutput, "Version 16.") <= 0) _
if (InStr(1, g_strShellOutput, "16.") <= 0) _
#line 1069
#And (InStr(1, g_strShellOutput, "Version 17.") <= 0) then
And (InStr(1, g_strShellOutput, "17.") <= 0) then
#line 1077
#if InStr(1, g_strShellOutput, "Version 16.") > 0 then
if InStr(1, g_strShellOutput, "16.") > 0 then
- 修改E:\Vbox\VirtualBox-5.0.0\Makefile.kmk,在最后一行追加
VBOX_BLD_PYTHON=E:\Python\python.exe
- 用記事本將以下文件另存為Unicode
E:\Vbox\VirtualBox-5.0.0\src\VBox\Devices\PC\DevLPC.cpp
E:\Vbox\VirtualBox-5.0.0\src\VBox\Disassembler\DisasmCore.cpp
E:\Vbox\VirtualBox-5.0.0\src\VBox\VMM\VMMAll\SELMAll.cpp
E:\Vbox\VirtualBox-5.0.0\src\VBox\Main\testcase\tstGuestCtrlParseBuffer.cpp
其中tstGuestCtrlParseBuffer.cpp修改編碼后會(huì)發(fā)現(xiàn)代碼有問題,將引號(hào)補(bǔ)齊如下
//line 88
{ "??f?\0\0", sizeof("??f?\0\0"), 0, sizeof("??f?\0") - 1, 1, VINF_SUCCESS },
{ "??f?\0????", sizeof("??f?\0????"), 0, sizeof("??f?\0") - 1, 1, VERR_MORE_DATA },
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 切換到vbox根目錄,如:E:\Vbox\VirtualBox-5.0.0
- 命令行輸入
cscript configure.vbs --with-libSDL=E:\Vbox\SDL-1.2.15 --with-openssl=E:\Vbox\openssl-1.0.1t-64\output --with-libcurl=E:\Vbox\curl-7.49.1-64 --with-QT4=E:\Vbox\qt-everywhere-opensource-src-4.7.3 --with-MinGW-w64=E:\Vbox\mingw64
- 修改E:\Vbox\VirtualBox-5.0.0\AutoConfig.kmk
#line 8
#PATH_TOOL_VCC100 := C:/PROGRA~2/MIB5D6~1.0/VC/BIN
PATH_TOOL_VCC100 := C:/PROGRA~2/MIB5D6~1.0/VC
- 命令行輸入
env.bat
kbuild\envwin.cmd --win64
kmk
運(yùn)氣好的話,等待1至2小時(shí)就編譯成功了,編譯好的Vbox在E:\Vbox\VirtualBox-5.0.0\out目錄下。
安裝Virtual Box
- 將以下dll拷貝到VirtualBox.exe所在目錄下,如: E:\Vbox\VirtualBox-5.0.0\out\win.x86\release\bin
libcurl.dll (e.g., from E:\Vbox\curl-7.49.1-64\lib\)
libeay32.dll (e.g., from E:\Vbox\openssl-1.0.1t-64\output\bin\)
ssleay32.dll (e.g., from E:\Vbox\openssl-1.0.1t-64\output\bin\)
QtCore4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtGui4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtNetwork4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtOpenGL4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
- 開始菜單->cmd->右鍵->以管理員身份運(yùn)行
- 切換到vbox根目錄,如:E:\Vbox\VirtualBox-5.0.0,命令行輸入
env.bat
- 切換到VirtualBox.exe所在目錄下,如: E:\Vbox\VirtualBox-5.0.0\out\win.x86\release\bin,命令行輸入
comregister.cmd
loadall.cmd
安裝完成(偽)
編譯驅(qū)動(dòng)程序簽名的Virtual Box
在經(jīng)歷上面一系列坑爹的步驟之后,相信大家在敲下loadall.cmd的時(shí)候都有一種終于解放了的爽快感。然而天不遂人愿,在loadall.cmd之后,我們馬上就會(huì)看見一堆無情的錯(cuò)誤。
這是因?yàn)樵趙in7 64位系統(tǒng)上,沒有經(jīng)過簽名認(rèn)證的驅(qū)動(dòng)程序是無法安裝的,所以我們需要在上面的編譯步驟里面加上簽名的步驟,然后整個(gè)重新編譯一次。
創(chuàng)建、安裝測(cè)試證書
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 命令行輸入
makecert.exe -r -pe -ss my -n "CN=MyTestCertificate" mytestcert.cer
certmgr.exe -add mytestcert.cer -s -r localMachine root
certmgr.exe
檢查在“個(gè)人”和“受信任的根證書頒發(fā)機(jī)構(gòu)”里面是否都有MyTestCertificate
- 開始菜單->cmd->右鍵->以管理員身份運(yùn)行
- 命令行輸入
Bcdedit.exe -set TESTSIGNING ON
這個(gè)據(jù)說是打開win7的調(diào)試模式,使用測(cè)試證書簽名的程序必須在這個(gè)模式下才能使用,可以使用下面的命令關(guān)閉調(diào)試模式。
Bcdedit.exe -set TESTSIGNING OFF
這個(gè)功能可能不能正常使用,我沒有遇到,所以也沒辦法提供解決方法。但是要注意我的筆記本是神舟戰(zhàn)神K660E D1,使用這個(gè)命令后直接無法進(jìn)入Bios,最后是將系統(tǒng)盤安裝在別的電腦上,還原了系統(tǒng)備份才解決。不過據(jù)了解,這個(gè)命令正常來說應(yīng)該是不會(huì)造成這種情況的,我的系統(tǒng)盤在別的電腦上系統(tǒng)也能正常啟動(dòng)。所以神舟戰(zhàn)神K660E D1這款電腦的Bios可能是個(gè)特例。下面繼續(xù):
certmgr.exe -add mytestcert.cer -s -r localMachine root
certmgr.exe -add mytestcert.cer -s -r localMachine trustedpublisher
重啟電腦,然后應(yīng)該能看見桌面右下角顯示“測(cè)試模式”。
重新編譯Vbox需要的dll
- 刪除前面除Vbox以外的所有東西,重新解壓新的文件,刪掉Vbox的out目錄,從頭開始。
- 修改E:\Vbox\qt-everywhere-opensource-src-4.7.3\mkspecs\win32-msvc2010\qmake.conf
#line 64
#QMAKE_LFLAGS_DLL = /DLL
QMAKE_LFLAGS_DLL = /DLL /INTEGRITYCHECK
- 重新編譯Qt
- 修改E:\Vbox\openssl-1.0.1t-64\ms\ntdll.mak
#line 34
#LFLAGS=/nologo /subsystem:console /opt:ref /debug
LFLAGS=/nologo /subsystem:console /opt:ref /debug /INTEGRITYCHECK
#line 51
#MLFLAGS= /nologo /subsystem:console /opt:ref /debug /dll
MLFLAGS= /nologo /subsystem:console /opt:ref /debug /dll /INTEGRITYCHECK
ntdll.mak 要在ms\do_win64a指令后才會(huì)有,因?yàn)槭切陆鈮旱模砸葓?zhí)行到ms\do_win64a為止。
- 重新編譯OpenSSL
- 修改E:\Vbox\curl-7.49.1-64\lib\Makefile.vc10
#line 114
#LNKDLL = link.exe /DLL
LNKDLL = link.exe /DLL //INTEGRITYCHECK
- 重新編譯curl
- 開始菜單->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio x64 兼容工具命令提示(2010)
- 使用
Signtool.exe sign –v [name].dll
對(duì)以下文件進(jìn)行簽名
libcurl.dll (e.g., from E:\Vbox\curl-7.49.1-64\lib\)
libeay32.dll (e.g., from E:\Vbox\openssl-1.0.1t-64\output\bin\)
ssleay32.dll (e.g., from E:\Vbox\openssl-1.0.1t-64\output\bin\)
QtCore4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtGui4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtNetwork4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
QtOpenGL4.dll (e.g., from E:\Vbox\qt-4.7.3\bin)
- 可以使用
Link.exe /dump /headers [name].dll
查看DLL characteristics下是否顯示Check integrity
- 修改E:\Vbox\VirtualBox-5.0.0\LocalConfig.kmk
#VBOX_WITH_HARDENING :=
VBOX_SIGNING_MODE=test
- 將以下文件復(fù)制到E:\Vbox\VirtualBox-5.0.0\tools\win.x86\selfsign\r1
C:\WinDDK\7600.16385.1\bin\selfsign\*
C:\WinDDK\7600.16385.1\bin\amd64\SignTool.exe
- 重新編譯并安裝Vbox
結(jié)語(yǔ)
- 至此Vbox編譯安裝結(jié)束,不過受限于驅(qū)動(dòng)簽名證書是測(cè)試證書,只能在開啟了TESTSIGNING模式的電腦上使用。如果想在別的電腦上正常使用,就去證書機(jī)構(gòu)申請(qǐng)證書吧。
- 上面的編譯步驟一開始是沒有做驅(qū)動(dòng)簽名的,之所以分開來寫,是想把編譯步驟區(qū)分開來,這樣其中一步出錯(cuò)了,能夠比較快地定位錯(cuò)誤。
- 編譯過程中可能會(huì)遇到很多編碼問題,根據(jù)提示一一修改為Unicode即可。
- 上述步驟里面并沒有加入webservice API和Java API,如果需要用到相應(yīng)API,參考Windows build instructions。
- 最后附贈(zèng)一篇編譯Win 32位Vbox的帖子
Building VirtualBox OSE 5.0.0 on Windows 7 (x86)。