用Visual Studio 2022 & nasm 编译 x264
这篇文字是用 MS Word 的共享功能,将docx文件直接发布到《博客园》,发布之后,发现有许多格式问题。
用Visual Studio 2022(VS17) & nasm 编译 x264
说明
本文完全是对 https://shiftmediaproject.github.io/ 的一个解读。高玩去直接阅读这个网站即可。
如果不想编译,可以去下载已经编译好文件。选择下载32位和64位的lib或dll各种版本。下载地址:
https://github.com/ShiftMediaProject/x264/releases
下载直接使用即可,后续的的文字可以不用看了。
如果没有发现合适的版本,或执意要用 VS17 重新编译 x264 。那么就需要先安装&配置 nasm 到 VS17中。
一般做法
在Windows中编译 x264的代码,目前一般是使用MSYS2中的Mingw-w64 gcc + nasm工具集。 但是,编译输出的x264 静态库,用于 Visual Studio MSVC 创建的项目中,静态库链接时,可能会u符号冲突等的问题。
下载nasm
nasm官网:https://www.nasm.us/
下载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
...\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
...\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
在Visual Studio 2022(VS17) 中配置nasm汇编工具
复制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
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\Hostx64\x86\nasm.exe
安装nasm 在 VS 中的配置文件
在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>
注意:安装如上的3个配置文件的同时,也会将其安装包中 nasm.exe 安装到如下目录中:
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\nasm.exe
但是似乎啥没用!
下载 x264 工程
x264 for VS工程下载地址:
https://github.com/ShiftMediaProject/x264
这个网页上,不止只有 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.
高玩已经将工程配置好了,编译过程很简单:
解压下载的文件,用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 ,OK! 其它 x264 版本没有测试。