用VisualStudio编译x264

《用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。其它版本还没有测试。 

posted on 2022-03-30 22:47  johnphan  阅读(1361)  评论(0编辑  收藏  举报

导航