使用MSYS2 + MinGW64,編譯32bit x264過程
(創建libx264-164.dll,2021年版)
Today is September 2021
網絡上的信息都有了一些新變化。茲記錄一下目前網絡資源環境下,使用MSYS2 + MinGW64,編譯x264過程,創建x264版本是libx264-164.dll。
l Windows10下安裝MSYS2 + MinGW64
利用msys2安裝mingw-w64過程:
按照 https://www.msys2.org/ 的提示安裝即可:下載並啟動文件:msys2-x86_64-20210725.exe,開始安裝。
下一步
在 MSYS2 MSYS(msys2_shell.cmd / msys2.exe)下安裝各種工具:
pacman -S mingw-w64-x86_64-toolchain(不編譯64bit 不需安裝這個)
pacman -S mingw-w64-i686-toolchain
pacman -S base-devel
pacman -S nasm(nasm用於編譯x264的匯編代碼)。
(提示:沒有安裝yasm!!!)
安裝完成之后,要注意msys2目錄下有:
clang64.exe |
|
mingw32.exe |
用mingw-w64-i686-toolchain 編譯。 |
mingw64.exe |
用mingw-w64-x86_64-toolchain編譯。 |
msys2.exe |
|
ucrt64.exe |
|
注意:安裝MSYS工具一定在MSYS2 MSYS(msys2_shell.cmd)窗口中進行。
啟動MSYS2 MinGW 64-bit ,在mingw64.exe窗口中看gcc的版本, host= x86_64-w64-mingw32。屏幕顯示如下:
$ gcc -v
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-10.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev5, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags='-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc' 'LDFLAGS_FOR_TARGET=-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high' --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ -pipe\ -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high\ -Wl,--stack,12582912'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Rev5, Built by MSYS2 project)
john@LAPTOP-FJNTRVPC MINGW64 ~
啟動 MSYS2 MinGW 32-bit,在mingw32.exe窗口中看gcc的版本, host=x86_64-w64-mingw32
john@LAPTOP-FJNTRVPC MINGW64 ~/x264-master
$ gcc -v
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-10.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev5, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags='-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc' 'LDFLAGS_FOR_TARGET=-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high' --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ -pipe\ -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high\ -Wl,--stack,12582912'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Rev5, Built by MSYS2 project)
查看nasm版本:
$ nasm -v
NASM version 2.15.05 compiled on Oct 24 2020
編譯x264
l 下載最新的x264代碼,
x264, the best H.264/AVC encoder - VideoLAN
https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
x264-master.tar.bz2
解壓到home/john 目錄下。
編譯64bit代碼,啟動 MSYS2 MinGW 64-bit 或者
編譯32bit代碼,啟動 MSYS2 MinGW 32-bit
l 開始編譯
在 MSYS2 MinGW 32-bit 環境下
$ ./configure --host=i686-w64-mingw32 --enable-shared --extra-ldflags=-Wl,--output-def=libx264.def
platform: X86
byte order: little-endian
system: WINDOWS
cli: yes
libx264: internal
shared: yes
static: no
bashcompletion: no
asm: yes
interlaced: yes
avs: avisynth
lavf: no
ffms: no
mp4: no
gpl: yes
thread: win32
opencl: yes
filters: crop select_every
lto: no
debug: no
gprof: no
strip: no
PIC: no
bit depth: all
chroma format: all
You can run 'make' or 'make fprofiled' now.
john@LAPTOP-FJNTRVPC MINGW32 ~/x264-master
$ make
dependency file generation...
windres --target=pe-i386 -I. -o x264res.o x264res.rc
gcc -Wno-maybe-uninitialized -Wshadow -O3 -ffast-math -m32 -Wall -I. -I. -D_POSIX_C_SOURCE=200112L -march=i686 -mfpmath=sse -msse -msse2 -std=gnu99 -D_GNU_SOURCE -mpreferred-stack-boundary=6 -fomit-frame-pointer -fno-tree-vectorize -fno-zero-initialized-in-bss -fvisibility=hidden -c x264.c -o x264.o
略
john@LAPTOP-FJNTRVPC MINGW32 ~/x264-master
$ make install
install -d /usr/local/bin
install x264.exe /usr/local/bin
install -d /usr/local/include /usr/local/lib/pkgconfig
install -m 644 ./x264.h x264_config.h /usr/local/include
install -m 644 x264.pc /usr/local/lib/pkgconfig
install -d /usr/local/lib
install -d /usr/local/bin
install -m 755 libx264-164.dll /usr/local/bin
install -m 644 libx264.dll.a /usr/local/lib
john@LAPTOP-FJNTRVPC MINGW32 ~/x264-master
john@LAPTOP-FJNTRVPC MINGW32 ~/x264-master
$ ls -lt
total 9572
-rwxr-xr-x 1 john john 3012773 Sep 29 00:31 libx264-164.dll
-rw-r--r-- 1 john john 1532 Sep 29 00:31 libx264.def
-rw-r--r-- 1 john john 34490 Sep 29 00:31 libx264.dll.a
-rw-r--r-- 1 john john 644 Sep 29 00:31 x264dll.o
-rw-r--r-- 1 john john 948 Sep 29 00:31 x264res.dll.o
-rwxr-xr-x 1 john john 3236994 Sep 29 00:31 x264.exe
-rw-r--r-- 1 john john 2951576 Sep 29 00:31 libx264.a
在cmd中
C:\msys64\home\john\x264-master>LIB /DEF:libx264.def
Microsoft (R) Library Manager Version 14.29.30133.0
Copyright (C) Microsoft Corporation. All rights reserved.
LINK : warning LNK4068: 未指定 /MACHINE;默認設置為 X86
正在創建庫 libx264.lib 和對象 libx264.exp
C:\msys64\home\john\x264-master>
返回 MSYS2 MinGW 32-bit窗口中:
john@LAPTOP-FJNTRVPC MINGW32 ~/x264-master
$ ls -lt
total 9584
-rw-r--r-- 1 john john 13106 Sep 29 00:38 libx264.lib
-rw-r--r-- 1 john john 7738 Sep 29 00:38 libx264.exp
-rwxr-xr-x 1 john john 3012773 Sep 29 00:31 libx264-164.dll
-rw-r--r-- 1 john john 1532 Sep 29 00:31 libx264.def
-rw-r--r-- 1 john john 34490 Sep 29 00:31 libx264.dll.a
-rw-r--r-- 1 john john 644 Sep 29 00:31 x264dll.o
-rw-r--r-- 1 john john 948 Sep 29 00:31 x264res.dll.o
-rwxr-xr-x 1 john john 3236994 Sep 29 00:31 x264.exe
-rw-r--r-- 1 john john 2951576 Sep 29 00:31 libx264.a
164是x264的版本號。將libx264-164.dll 文件改名libx264.dll和libx264.lib文件x264.h,x264_config.h一並放置測試工程的相應目錄,編譯測試工程中會發現還缺少如下dll文件:libgcc_s_dw2-1.dll,libwinpthread-1.dll,等。從MSYS2復制它們到測試工程的相應目錄,測試工程編譯成功。
附錄測試代碼:
1 #include <iostream> 2 #include <string> 3 #include "stdint.h" 4 5 #pragma comment(lib, "libx264.lib") 6 7 extern "C" 8 { 9 #include ".\x264.h" 10 #include "x264_config.h" 11 }; 12 13 using namespace std; 14 15 int main() 16 { 17 std::cout << "Hello World!\n"; 18 x264_param_t param; 19 x264_param_default(¶m); 20 cout << "hello,x264" << endl; 21 } 22 23