《用VisualStudio編譯x264》
用VC+ nasm 編譯 x264,不用gcc編譯。
x264 版本:164
VC版本:Visual Studio Community 2022 版本 Vesrsion17.1.0 ( v143 )
說明
本文完全是對 https://shiftmediaproject.github.io/ 的一個學習和實踐記錄。高玩可以去直接閱讀這個網站即可。
如果不想用VC編譯,也是可以去下載已經編譯好文件。選擇下載32位和64位的lib或dll各種版本。下載地址:
https://github.com/ShiftMediaProject/x264/releases
如果下載直接使用即可,后續的的文字可以不用看了。
如果沒有發現合適的可下載版本,或執意要用VC重新編譯 x264 。那么就需要先安裝 & 配置 nasm 到 VC中。
通常做法:x264一般都是用gcc編譯
在 Windows 中編譯 x264的代碼,目前一般是使用MSYS2中的Mingw-w64 gcc + nasm工具集。參考:
使用MSYS2 + MinGW64 編譯 x264 過程
但是,編譯輸出的x264 靜態庫,用於 VC 創建的 非dll 多任務項目中,靜態庫鏈接時,會符號沖突的問題.。深入x264庫內部去調試也可能會問題。這里描述的是直接用VC編譯x264的過程。
下載nasm
nasm官網:https://www.nasm.us/ 有32/64版本可選。
如果想下載win32版本:https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win32/
C:\Users\jiangfan\Downloads\Compressed\nasm-2.15.05-win32\nasm-2.15.05 的目錄
nasm-2.15.05-win32.zip
2020/08/28 09:04 1,550 LICENSE
2020/08/28 09:08 1,263,104 nasm.exe
2020/08/28 09:08 711,680 ndisasm.exe
2022/02/21 14:56 <DIR> rdoff
...\nasm-2.15.05-win32\nasm-2.15.05>nasm -v
NASM version 2.15.05 compiled on Aug 28 2020
如果想下載win64版本:https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/
nasm-2.15.05-win64.zip
2020/08/28 09:04 1,550 LICENSE
2020/08/28 09:08 1,388,544 nasm.exe
2020/08/28 09:08 952,832 ndisasm.exe
2022/02/21 14:56 <DIR> rdoff
...\nasm-2.15.05-win64\nasm-2.15.05>nasm -v
NASM version 2.15.05 compiled on Aug 28 2020
win32 & win64 均有一個安裝包可下載(下圖),但是建議,不要選擇其安裝nasm,因為其只能集成VS8(VS2005),並且除了nasm.exe 之外,nasm 的其它部分暫時並不需要:
nasm-2.15.05-installer-x32.exe
nasm-2.15.05-installer-x64.exe
備注:如果已經安裝有 MSYS2,也可以在MSYS2中安裝nasm:
pacman -S nasm
$ nasm -v
NASM version 2.15.05 compiled on Oct 24 2020
配置nasm匯編工具到Visual Studio 2022(VS17) 中
復制nasm.exe
將下載的nasm-2.15.05-win64.zip 解壓,nasm.exe 復制到VS的目錄中:(是選擇win32 or win64 ???)
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\Hostx86\x86\nasm.exe
遇到的問題備忘:這個目錄何用???
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\nasm.exe
安裝nasm 在 VS 中的配置文件,將nasm集成到VC中
在MSVC中使用 nasm ,需要3個配置文件:
D:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Microsoft\VC\v170\BuildCustomizations\nasm.props
D:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Microsoft\VC\v170\BuildCustomizations\nasm.targets
D:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Microsoft\VC\v170\BuildCustomizations\nasm.xml
幸好已經有高玩設計好了這3個文件,網絡下載安裝即可。
https://github.com/ShiftMediaProject/VSNASM
下載 VSNASM-master.zip 。注意:這個安裝包中附有一個 nasm.exe 的壓縮包。
解壓VSNASM-master.zip,以管理員身份啟動CMD ,運行 install_script.bat
D:\NASM\VSNASM-master>install_script.bat
Detected 64 bit system...
Using existing vswhere binary...
Visual Studio 2022 environment detected...
Installing VS17 customisations into "d:\Program Files\Microsoft Visual Studio\2022\Community"
Installing build customisations...
Using existing NASM binary...
Installing required NASM release binary...
Finished Successfully
請按任意鍵繼續. . .
D:\NASM\VSNASM-master>
集成OK,可以在VisualStudio中使用nasm了。
注意:安裝如上的3個配置文件的同時,也會將其安裝包中 nasm.exe 安裝到如下目錄中:
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\nasm.exe
但是似乎啥沒用!
下載 x264 源代碼
x264 for VS工程下載地址:
https://github.com/ShiftMediaProject/x264
X264 版本 164
這個網頁上,不止只有 x264 一個工程,其簡介如下:
https://shiftmediaproject.github.io/
Shift Media Project aims to provide native Windows development libraries for FFmpeg and associated dependencies to support simpler creation and debugging of rich media content directly within Visual Studio.
高玩已經將工程 for VC 配置好了,編譯過程也就很簡單了:
解壓下載的文件,用 VS17 打開項目文件。
...\x264-master\SMP\x264.sln
忽略打開項目時提示 WinRT 錯誤:
Project 'libx264_winrt' could not be loaded because it's missing install components. To fix this launch Visual Studio installer with the following selections:
C++ (v143) Universal Windows Platform tools
...\x264-masterforVS\x264-master\SMP\libx264_winrt.vcxproj : error : Project 'libx264_winrt' could not be loaded because it's missing install components. To fix this launch Visual Studio installer with the following selections:
C++ (v143) Universal Windows Platform tools
選擇各種版本,開始編譯 libx264工程(其它工程不用編譯),按照要求編譯各種版本的lib或dll(x86/x64)輸出。 也可以配置自己需要的版本。
在實際工程中,只是測試編譯出的x86 Release 版本 libx264.lib。其它版本還沒有測試。